码迷,mamicode.com
首页 > 编程语言 > 详细

SpringInAction4笔记——装配

时间:2017-08-07 11:55:35      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:ted   指定   lis   blank   port   常量   int   gic   导入   

重点:常用的上下文环境

AnnotationConfigApplicationContext

ClassPathXmlApplicationContext

FileSystemXmlApplicationContext


AnnotationConfigWebApplicationContext

XmlWebConfigApplicationContext

 

1.常用的注解

 

  @Autowired

  @Qualifier("sgtPeppers" )

  private CompactDisc sgtPeppers;

 

@Component

 

 

 

 

@Autowired是spring的元注解,可以用java的@Inject替代,先安类型找,如果找不到或者有多个,就按照name找,(扫描时默认是把类名第一个字母小写作为name)

可以用@Qualifier来指定name,或者把变量名改成扫描时生产的name

解决自动装配的歧义性,可以通过

@Qualifier 推荐用这个,设置一个代表性的名字

@Primary 

 

 

 

@Configuration

 

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(classes=CDPlayerConfig.class)

 

 

xml用构造器注入时,

<constructor-arg value="The Beatles" />

或者用c语法

 

 

<bean id="compactDisc" class="soundsystem.BlankDisc"

 

        c:_0="Sgt. Pepper‘s Lonely Hearts Club Band" 

 

        c:_1="The Beatles" />

 

        

 

  <bean id="cdPlayer" class="soundsystem.CDPlayer"

 

        c:_-ref="compactDisc" />

 

 

注入属性时:

<property name="artist" value="The Beatles" />

或者用p语法

 <bean id="compactDisc"

        class="soundsystem.properties.BlankDisc"

        p:title="Sgt. Pepper‘s Lonely Hearts Club Band"

        p:artist="The Beatles"

        p:tracks-ref="trackList" />

 

<util:list>等来将bean中的复杂的参数移到外面去,保持xml代码的易读性

 

 

xml中引用其他xml文件

 

<import resource="cd-config.xml">

 

可以使用<bean class="xxx.xx">把javaConfig类导入到XML文件中

 

javaConfig中引用其他配置

 

 

@Configuration

 

@Import(CDPlayerConfig.class)

 

@ImportResource("classpath:cd-config.xml")

 

public class SoundSystemConfig {

 

 

 

}

 

 

 

无论怎么引用,只要在root配置中启用组件扫描即可

比如@ComponentScan和<context:component-scan >

 

配置多个可互相替代的bean时,比如DataSource

java配置:

 

 

@Profile("dev")

xml配置

<beans profile="dev">

 

 

使用时通过@ActiveProfiles来选择用哪个

 

@ActiveProfiles("prod")

 

 

也可以在

在web.xml中,配置

<context-param>

<param-name></param-name>

<param-value></param-value>

</context-param>

 

按条件注入Bean

 

 

1、@Bean

@Conditional(MagicExistsCondition.class)

 

 

 

 

2、public class MagicExistsCondition implements Condition

 

 

Bean的作用域

 单例 Singleton

原型 Prototype

会话 Session

请求 Request

 

 

@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)

 

 

 

<bean class="com.myapp.Notepad" scope="prototype" />

 

 

会话 Session和请求 Request需要设置代理proxyMode,

如果是xml配置,bean中加入<aop:scoped-proxy>

 

 

生命周期 

1.实例化

2.为属性赋值

3.BeanNameAware接口

4.BeanFactoryAware接口

5.ApplicationContextAware接口

6.BeanPostProcessor接口

7.InitializingBean接口

8.DisposableBean接口

 

 

 


引入properties文件

 

 

 

<context:property-placeholder location="classpath:system.properties" />

 

 

 

java配置

 

@PropertySource("classpath:/com/soundsystem/app.properties")

 

 

占位符${...}

SpEL表达式#{...},支持类型安全运输符 "?."

访问类作用域的方法和常量,需要依赖T关键字,比如#{T(System).currentTimeMillis()}

 

SpEL表达式还有很多知识点,不再赘述

使用@Value注解来调用${...}和#{...}

@Value("#{systemPrpperties[‘disc.title‘]}") String title

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

SpringInAction4笔记——装配

标签:ted   指定   lis   blank   port   常量   int   gic   导入   

原文地址:http://www.cnblogs.com/lakeslove/p/7297903.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!