Spring中IOC的Annotation实现:
此处特别注意每个类中注释的注解及相关文字说明,可以更好的理解Spring中IOC的Annotation实现!
首先beans.xml中需要引入 context 的schema:
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
在XML中配置Spring对Annotation的相关配置:
<!-- 打开Spring的Annotation的支持 --> <context:annotation-config /> <!-- 设定Spring去哪些包中找Annotation --> <context:component-scan base-package="com.spring" />
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <!-- 打开Spring的Annotation的支持 --> <context:annotation-config /> <!-- 设定Spring去哪些包中找Annotation --> <context:component-scan base-package="com.spring" /> </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 org.springframework.stereotype.Component; import org.springframework.stereotype.Repository; import com.spring.model.User; //等于完成了<bean id="userDao" class="com.spring.dao.UserDao"/> //@Component("userDao") //@Component公共的创建bean的Annotation @Repository("userDao") //@Repository表示一般用于Dao的注入 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); } }
package com.spring.service; import com.spring.model.User; public interface IUserService { public void add(User user); public void delete(int id); public User load(int id); }
package com.spring.service; import javax.annotation.Resource; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; import com.spring.dao.IUserDao; import com.spring.model.User; //@Component(value="userService") @Service("userService") //业务层一般用@Service注入 public class UserService implements IUserService { private IUserDao userDao; public IUserDao getUserDao() { return userDao; } //默认通过名称注入,在JSR330中提供了@Inject来注入 @Resource public void setUserDao(IUserDao userDao) { this.userDao = userDao; } @Override public void add(User user) { userDao.add(user); } @Override public void delete(int id) { userDao.delete(id); } @Override public User load(int id) { return userDao.load(id); } }
package com.spring.action; import javax.annotation.Resource; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; import org.springframework.stereotype.Controller; import com.spring.model.User; import com.spring.service.IUserService; //@Component("userAction") @Controller("userAction") //MVC的控制层一般使用@Controller @Scope("prototype") //设置多例 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; } @Resource 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.User; public class TestSpring { //创建Spring工厂 private BeanFactory beanFactory=new ClassPathXmlApplicationContext("beans.xml"); @Test public void testUser(){ UserAction ua=beanFactory.getBean("userAction",UserAction.class); User user=new User(1,"我叫spring"); ua.setUser(user); ua.add(); //单例的时候添加相同的内容,多例添加为null UserAction ua1=beanFactory.getBean("userAction",UserAction.class); ua1.add(); } }