标签:
Spring 的IoC方式有很多种,下面一一进行说明
setter方法注入
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="personDao" class="org.zln.module.test2.dao.impl.PersonDaoBean"/> <bean id="personService" class="org.zln.module.test2.service.impl.PersonServiceBean"> <property name="personDao" ref="personDao"/> </bean> </beans>
构造函数注入
<bean> <constructor index="" type="" value|ref=""/> </bean>
内部Bean
<bean id="" class=""> <property name=""> <bean class=""/><!-- 这个Bean就只有这个类能够使用 --> </property> </bean>
注入集合类型
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"> <property name="sets"> <set> <value>第一个</value> <value>第二个</value> <value>第三个</value> </set> </property> <property name="lists"> <list> <value>第一个list元素</value> <value>第二个list元素</value> <value>第三个list元素</value> </list> </property> <property name="properties"> <props> <prop key="key1">value1</prop> <prop key="key2">value2</prop> <prop key="key3">value3</prop> </props> </property> <property name="maps"> <map> <entry key="key-1" value="value-1"/> <entry key="key-2" value="value-2"/> <entry key="key-3" value="value-3"/> </map> </property> </bean> </beans>
注解方式进行注入:
@Resource注解与@AutoWired注解
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <!--这个配置,隐式注册了多个对注解进行解析的处理器--> <context:annotation-config/> <bean id="personService" class="org.zln.module.test2.service.impl.PersonServiceBean" /> </beans>
package org.zln.module.test2.service.impl; import org.apache.log4j.Logger; import org.zln.module.test2.dao.PersonDao; import org.zln.module.test2.service.PersonService; import javax.annotation.Resource; /** * Created by coolkid on 2015/6/6 0006. */ public class PersonServiceBean implements PersonService { private Logger logger = Logger.getLogger(PersonServiceBean.class); @Resource private PersonDao personDao; public PersonServiceBean() { logger.debug("默认构造函数进行实例化"); } @Override public void save(){ logger.debug("我是save()方法"); personDao.add(); } } /* Resource默认按照名称进行装配,也就是说会从Spring容器中查找名称相同的实例进行注入,当找不到同名实例的时候,再按照类型进行查找与注入 如果手工指定了名称或者类型 Resource(name="personDao") 或 Resource(type="java.lang.String") 则只会按照指定的名称或类型进行查找。找不到就算了 */
package org.zln.module.test2.service.impl; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.zln.module.test2.dao.PersonDao; import org.zln.module.test2.service.PersonService; import javax.annotation.Resource; /** * Created by coolkid on 2015/6/6 0006. */ public class PersonServiceBean implements PersonService { private Logger logger = Logger.getLogger(PersonServiceBean.class); @Autowired private PersonDao personDao; @Override public void save(){ logger.debug("我是save()方法"); personDao.add(); } } /* 使用AutoWired注入与Resource注入的区别在于,AutoWired默认使用类型查找注入,默认依赖对象不允许为null @Autowired(required = false)这样设置后就运行依赖对象可以为空了 一般就使用Resource进行注解注入 */
自动扫描
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <!-- 自动扫描该包及其子包下的类,并纳入Spring容器 --> <context:component-scan base-package="org.zln.module.test2"/> </beans> <!-- 在包下,所有被 @Service @Controller @Repositry @Component 注解的类才能被扫描并纳入管理 @Service 标注业务层组件 @Controller 标注控制层,如Struts中的Action @Repositry 标注Dao层 @Component 不好归类的时候使用这个注解 被管理的bean的名称,默认规则是 小写首字母的类名 如果想要手动指定名称 @Service("xxx") xxx就是新的纳入Spring容器的该类的id 如果想要修改作用域,需要使用@Scope("prototype") 注解 指定某个方法在实例化后立马执行,在该方法上加 @PostConstruct 销毁前执行方法,加 @PreDestrcy 注解 声明 <context:component-scan base-package="org.zln.module.test2"/> 后,就不需要 <context:annotation-config/> 了,因为对注解的处理器重复了 -->
标签:
原文地址:http://www.cnblogs.com/sherrykid/p/4573923.html