Spring2.5依靠注入的方式有三种: 1.通过setter方法注入; 2.通过构造方法注入; 3.通过注解进行注入; 第一种方式:通过setter方法注入 Java代码 package com.test; public class UserServiceImplement implements IUserService { private IUserDao user; public IUserDao getUser() { return user; } public void setUser(IUserDao user) { this.user = user; } public void saveUser() { user.addUser(); } } Xml代码 <?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:lang="http://www.springframework.org/schema/lang" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.0.xsd"> <bean id="userdao" class="com.test.UserDaoImplement"></bean> <bean id="userservice" class="com.test.UserServiceImplement"> <property name="user" ref="userdao"></property> </bean> </beans> 注重这种方法还可以把XML改成以下方式: Xml代码 <?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:lang="http://www.springframework.org/schema/lang" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.0.xsd"> <bean id="userservice" class="com.test.UserServiceImplement"> <!-- <property name="user" ref="userdao"></property>--> <property name="user"> <bean class="com.test.UserDaoImplement"></bean> </property> </bean> </beans> 这两者的区别在于,第二种方式只会注入到当前这个Bean实例中,而不能再注入到其它的Bean中,第一种方式被注入的Bean可以注入到多个Bean当中,这可能会有一些线程安全的问题,因此在最好指定被注入的Bean的作用域。 另外通过setter方式注入,对应的注入依靠属性,必须要有setter方法。 测试代码: Java代码 package com.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext( "com/test/bean.xml"); IUserService us = (IUserService) ctx.getBean("userservice"); us.saveUser(); } } 第二种方式:通过构造方法进行注入 Java代码 package com.test; public class UserServiceImplement implements IUserService { private IUserDao user; int age; public UserServiceImplement(IUserDao user, int age) { this.user = user; this.age = age; } public void saveUser() { user.addUser(); System.out.println(this.age); } } Xml代码 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:lang="http://www.springframework.org/schema/lang" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.0.xsd"> <bean id="userdao" class="com.test.UserDaoImplement"></bean> <bean id="userservice" class="com.test.UserServiceImplement"> <constructor-arg index="0" type="com.test.IUserDao" ref="userdao"></constructor-arg> <constructor-arg index="1" value="24"></constructor-arg> </bean> </beans> 注重 <construcotr-arg>标签中的index属性假如构造方法只有一个参数的时候可以不指定,它的下标从0开始,表示构造方法参数的索引,假如有构造方法有多个参数必须指定索引。除此之外还有一个type属性,这个属性用来指定被注入的参数的参数类型,一定要跟构造方法中的参数类型一致,假如是一个接口,那么也不答应传它的实现类,这是一个可选属性; 假如构造函数的的参数类型是基本数据类型,那么就不用ref属性了,而用value属性设置它的值,而且这些数据类型会自动打包和解包; 同样要注重bean的作用域问题。 测试代码同上...... 第三种方式:通过java注解进行注入 使用这种方式进行注入可以用两个标注: @Resource @Autowired 两者的区别:1. @Resource标注是由JDK提供的,而@Autowired标注是由Spring提供的,因而@Autowired标注会与Spring紧密耦合,所以推荐使用@Resource标注; 2. @Resource默认是按照名称来装配注入的,当找不到与名称匹配的bean才会按照类型来装配注入; 3. @Autowired默认是按照类型装配注入的,假如想按照名称来转配注入,则需要结合@Qualifier一起使用; 4. @Resource和@Autowired都可以用来标注字段或者setter方法。 I.使用@Resource标注进行注入 Java代码 package com.test; import javax.annotation.Resource; public class UserServiceImplement implements IUserService { @Resource private IUserDao user; private IUserDao user1; public IUserDao getUser1() { return user1; } @Resource public void setUser1(IUserDao user1) { this.user1 = user1; } public void saveUser() { user.addUser(); System.out.println("user注入成功"); user1.addUser(); System.out.println("user1注入成功"); } } Xml代码 <?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="user" class="com.test.UserDaoImplement"></bean> <bean id="user1" class="com.test.UserDaoImplement"></bean> <bean id="userservice" class="com.test.UserServiceImplement"></bean> </beans> 测试类: Java代码 package com.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext( "com/test/bean.xml"); IUserService us = (IUserService) ctx.getBean("userservice"); us.saveUser(); } } II.使用@Autowired Java代码 package com.test; import org.springframework.beans.factory.annotation.Autowired; public class UserServiceImplement implements IUserService { @Autowired private IUserDao user; public IUserDao getUser() { return user; } public void setUser(IUserDao user) { this.user = user; } public void saveUser() { user.addUser(); System.out.println("user注入成功"); } } Xml代码 <?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="user" class="com.test.UserDaoImplement"></bean> <bean id="userservice" class="com.test.UserServiceImplement"></bean> </beans> 测试类: Java代码 package com.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext( "com/test/bean.xml"); IUserService us = (IUserService) ctx.getBean("userservice"); us.saveUser(); } }