操作步骤: 1.将所有的类在beans.xml 中创建 2.对有依赖的类完成注入 2.1.为每一个依赖类创建相应的Getter和setter 方法 <bean id="userAction" class="com.spring.action.UserAction" scope="prototype"> <!-- 同样action也需要注入 --> <property name="userService" ref="userService" /> </bean> 2.2.构造函数注入 <!-- 以下是使用构造函数来注入,不常用,基本都是用set 方法注入 --> <bean id="userAction" class="com.spring.action.UserAction" scope="prototype"> <constructor-arg ref="userService" /> </bean> 2.3.自动注入(不常用) 开发中一般都是使用byName。 <bean id="userService" class="com.spring.service.UserService" autowire="default"> autowire="default",byName:根据名称来注入(setUserDao,名称为userDao); byType:根据类型注入(存在多个相同类型的对象就会抛出异常,不知道要注入那个);no:不注入。 虽然自动注入可以减少配置,但是通过bean文件无法很好的了解整个类的结果,所以不建议使用autowire。 2.4.属性注入 <bean id="user" class="com.spring.model.User"> <property name="id" value="1"></property> <property name="username" value="我是spring"></property> <!--同时可以列表注入,但不常用--> <property name="names"> <list> <value>1111</value> <value>2222</value> <value>3333</value> </list> </property> </bean> 3.单例和多例(默认是单例) 3.1.当属性值的状态不会发生变化的时候我们用单例。(如:dao,Service) 3.2.对于Action而言,里面的属性值得状态会根据不同的线程得到不同的值,所以应该使用多例(Action)。
beans.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 创建如下bean等与完成了 HelloWorld helloWorld=new HelloWorld() --> <bean id="helloWorld" class="com.spring.model.HelloWorld" scope="prototype" /> <bean id="user" class="com.spring.model.User"> <property name="id" value="1"></property> <property name="username" value="我是spring"></property> </bean> <bean id="userDao" class="com.spring.dao.UserDao"></bean> <bean id="userService" class="com.spring.service.UserService"> <!-- name中的值会在userService 对象中调用setXX方法来注入,诸如:name="userDao" 在具体注入时会调用setUserDao(IUserDao userDao) 来完成注入; ref="userDao" 表示是配置文件中的bean中所创建的Dao的id,这样就完成了依赖注入。 --> <property name="userDao" ref="userDao" /> </bean> <bean id="userAction" class="com.spring.action.UserAction" scope="prototype"> <!-- 同样action也需要注入 --> <property name="userService" ref="userService" /> </bean> </beans>
package com.spring.model; public class User { public User() { super(); } private int id; private String username; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } @Override public String toString() { return "User [id=" + id + ", username=" + username + "]"; } public User(int id, String username) { super(); this.id = id; this.username = username; } }
package com.spring.dao; import com.spring.model.User; public interface IUserDao { public void add(User user); public void delete(int id); public User load(int id); }
package com.spring.dao; import com.spring.model.User; public class UserDao implements IUserDao { @Override public void delete(int id) { System.out.println("删除了"+id); } @Override public User load(int id) { System.out.println("load了User"); return null; } @Override public void add(User user) { System.out.println("添加了"+user); } }
server层类似于dao层,此处省略......
UserAction.class
package com.spring.action; import com.spring.model.User; import com.spring.service.IUserService; public class UserAction { private User user; private IUserService userService; private int id; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public IUserService getUserService() { return userService; } public void setUserService(IUserService userService) { this.userService = userService; } public int getId() { return id; } public void setId(int id) { this.id = id; } public void add(){ userService.add(user); } public void delete(){ userService.delete(id); } public void load(){ userService.load(id); } }
package com.spring.test; import org.junit.Test; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.spring.action.UserAction; import com.spring.model.HelloWorld; import com.spring.model.User; public class TestSpring { //创建Spring工厂 private BeanFactory beanFactory=new ClassPathXmlApplicationContext("beans.xml"); @Test public void testHello(){ //通过工厂获取Spring的对象 //此处getBean中的helloWorld 就是beans.xml中的id HelloWorld hello1=(HelloWorld)beanFactory.getBean("helloWorld"); HelloWorld hello2=beanFactory.getBean("helloWorld",HelloWorld.class); //此时的hello1对象就是被Spring管理的对象 System.out.println(hello1.hello()); //如果在bean中没有做scope的配置,默认是(singleton)单例 System.out.println(hello1==hello2); } @Test public void testUser(){ UserAction ua=beanFactory.getBean("userAction",UserAction.class); User user=new User(1,"我叫spring"); ua.setUser(user); ua.add(); } }
添加了User [id=1, username=我叫spring]