spring既要(-)

来源:互联网 发布:js涂膜防水施工工艺 编辑:程序博客网 时间:2024/05/29 17:44

Bean的装配

自动装配:通过组件扫描和自动注入
javaConfig:java 的@Config 类,@Bean注解的方法
xml :xml文件注入依赖

尽量使用自动装配

自动装配

@Componentpublic class XXX {    @Autoware    private YYY yyy;}@Configuration@ComponentScanpublic class XXXConfig {}

ComponentScan默认扫描本包和本包下属包。
或使用xml配置扫描

<context:component-scan base-package="com.xx"/>

可以使用@Named替代@Component,也可以用@Qualifier

Autowired可以作用到属性、set方法、构造方法中,可以和@Qualifier合用,以标明具体使用哪个bean

@ImportResource可以引入xml配置

@Import可以引入配置类

@Bean可以使用方法生产bean

bean的作用域

@Scope与@Component和@Bean一起使用

singleton:单例模式prototype:原型模式request:每次HTTP请求session:HTTP Session
接口代理模式,目标只能是接口@Scope(value=WebApplicationContext.SCOPE_SESSION,    proxyMode=ScopedProxyMode.INTERFACES)<aop:scoped-proxy proxy-target-class="false"/>或类代理模式ScopeProxyMode.TAGER_CLASS

注入外部属性值

@PropertySource(“xxx:xxx.properties”)

@Autowire
Enviroment env;
env.getProperty(“xxx”)

xml中加载属性文件

<context:property-placeholder file-encoding="UTF-8" location="classpath:/xxx1.properties,classpath:/xxx2.properties" />

SpEL

T(XXX).xxx() 引入类的静态方法和属性
systemProperties[‘xxx’] 配置的xxx属性值

profile 和 条件化创建Bean

profile使用

在@Configuration的类上设置@Profile(“xxx”)属性,表示类里的bean只有在xxx有效时才创建

同样@Profile可以配置到@Bean修饰的方法上,表示这个bean只有在这个prifile有效时才会创建,不会影响class中的其他类

可以在xml的上修改profile=”xxx”

profile的生效

dispatcherServlet的初始化参数
webxml的上下文
环境变量
jvm参数
测试类的@ActiveProfiles中

先查找spring.profiles.active,未找到再查找spring.profiles.default,以确定哪个profile生效.
多个profile生效时,使用逗号分割

条件@Conditional

可以加到@Bean的方法上

@Bean@Conditional(XXX.class)这个class需要实现Condition接口

首选bean

@Primary可以和@Component和@Bean配置,以确定同类型bean,首先使用这个bean

xml中可以在 中设置 primary = “true”

context类型

AnnotationConfigApplicationContext;AnnotationConfigWebApplicationContext;ClassPathXmlApplicationContext;FileSystemXmlApplicationContext;XmlWebApplicationContext;

AOP类型

配置文件方式

<aop:config>  <aop:aspect id="time" ref="timeHandler">    <aop:pointcut expression="execution(* *.service.impl.*.*(..))" id="pointcut"/>    <aop:before method="before" pointcut-ref="pointcut"/>    <aop:after method="after" pointcut-ref="pointcut"/>    <aop:after-returning method="afterReturning" pointcut-ref="pointcut"/>    <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut"/>    <aop:around method="around" pointcut-ref="pointcut"/>  </aop:aspect></aop:config>

注解方式

@Configuration@ComponentScan(basePackages = {"com.sun.demo.autoconfig","com.sun.demo.autoconfig.hello"})@EnableAspectJAutoProxy // 启用aop注解public class HelloConfig {    @Bean("hellox3")    public HelloWorld getHello3() {        return new HelloWorld3Impl();    }    @Bean("hello3")    public HelloWorld getHello() {        return new HelloWorld1Impl();    }}@Aspectpublic class HelloWorld3Impl implements HelloWorld {    public void printHelloWorld()    {        System.out.println("Enter HelloWorldImpl2.printHelloWorld()");    }    @Pointcut("execution(* com.sun.demo.aop.HelloWorld.*(..)")    public void pointcut() {    }    @Before("pointcut()")    public void doPrint()    {        System.out.println("Enter HelloWorldImpl2.doPrint()");        return ;    }}