您的位置:首页 > 编程语言 > Java开发

Spring中的Ioc

2016-04-07 14:00 633 查看
1、属性注入

需要提供一个默认的构造函数及需要注入的属性的Setter方法。

<bean id="car" class="com.xxx.Car">
<property name="brand" value="宝马"/> <span style="color:#cc0000;">//这里的name变量前两个字母要么全部大写,要么全部小写</span>
</bean>
2、构造函数注入

<bean id="car" class="com.xxx.Car">
<constructor-arg index="0" type="java.lang.String">//根据需要选择索引/类型方式
<value>宝马</value>>
</constructor-arg>
</bean>
此外,还可通过自身类型反射匹配<ref bean="other-beanId"/>

构造函数注入要注意循环依赖问题,可通过属性注入方式解决

3、注入参数

字面值需要注意特殊字符($,<,>,",')共5个,可通过转义解决或者加上<![CDATA[...]]>解决,标示把...当作普通字面处理

4、引用其它bean

<property name="car">
<ref bean="other-beanId">//bean:引用同一容器或父容器的Bean;local:只能引用同一配置文件中的bean;parent:引用父容器的bean
</ref>
</property>
5、内部bean

<property name="car">
<bean class="xxx.xxx.xxx">
<property name....>
</bean>
</property>
6、null值

<property name="car"><null/></property>
7、级联属性注入

<property name="car.brand" value="宝马">
</property>
表示对本bean调用getCar.setBrand("宝马")进行注入

8、集合类型,集合合并



9、集合类型的Bean

使用util命名空间

10、简化配置

<property name="car" value="宝马"/>


<property name="car" ref="other-beanId"/>
注意:使用简化配置时无法使用<![CDATA[...]]>

11、p命名空间

<bean id="car" class="com.xxx.Car">
p:brand="宝马"  //p:<属性名>="xxx"
p:price="1000000"
</bean>
<bean id="boss" class="com.xxx.Boss">
p:car-ref="car"   //p:<属性名>-ref="car"
</bean>
12、方法注入

<bean id="car" class="com.xxx.Car">
p:brand="宝马"
p:price="1000000"
scope="prototype"
</bean>
<bean id="magicBoss" class="com.xxx.megicBoss">
<lookup-method name="getcar" bean="car"> //每次调用getCar方法,都返回新的car实例
</bean>
13、父子bean

通过abstract="true"说明不实例化该bean,其它bean可以通过parent="abstract-beanId"来继承

14、依赖

通过依赖depends-on="front-beanId",可以指定本bean在front-beanId之后被实例化

15、Bean作用域

配置方式:scope="<作用域类型>",默认为singleton,容器起来时会自动实例化所有的singleton的Bean,当然可以通过lazy-init="true"来延迟实例化。

prototype作用域表示非单实例的Bean,所有引用的bean或者通过getBean方法也是返回一个新的实例。

针对web的作用域:request(每次http请求创建一个新的bean)、session(每个不同的session创建一个新的bean)、globalSession。ContextLoaderListener用于监听web容器的启动和关闭,但要实现web的作用域,必须要监听web 事件,所以在用这些作用域时,需要额外添加RequestContextListener

<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>>作用域依赖:如一个singleton的作用域依赖一个request的作用域,需要将request作用域的bean添加<aop:scoped-proxy/>创建代理,这样就能在获取bean时根据singleton bean的线程找到对应的httpRequest,再获取该request中的bean

16、基于注解的配置
(1)@Component(@Repository,@Service,@Controller)

(2)扫描时可以通过<context:component-san base-package="xxx" resource-pattern="anno/*.class"/>

(3)<context:include-filter type="type" expression="该类型的表达式">

(4)@Autowired进行自动注入,默认按类型匹配的方式,当有且仅有一个匹配的bean时,注入。@Autowired(required=false),表示找不到对应的bean时也不抛异常。当有多个相同类型的bean时通过@Autowired+@Qualifier("userDao")来限定bean的名称。如果通过@Autowired对集合类进行标注,则会将容器中匹配集合元素类型的所有bean都注入进来。

(5)@Scope("prototype"),与配置文件中类似

(6)@PostConstruct @PostDestroy可以设置多个,与配置中的init-method destroy-method类似

17、基于Java类的配置



18、使用外部属性文件

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
<property name="locations">
<list>
<value>classpath:sys.properties</value>
<value>classpath:mysql.properties</value>
<value>classpath:api.properties</value>
</list>
</property>
</bean>这样就可以在xml中通过"${变量}"的方式来引用配置文件中的值。在基于注解和java类的配置中则通过@Value("${变量}")

19、加密属性文件


20、引用bean的属性值

#{beanName.beanProp}

21、国际化

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring