首页 > 代码库 > Spring中IOC的Annotation实现
Spring中IOC的Annotation实现
Spring中IOC的Annotation实现:
此处特别注意每个类中注释的注解及相关文字说明,可以更好的理解Spring中IOC的Annotation实现!
首先beans.xml中需要引入 context 的schema:
[html] view plaincopyprint?
- <?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的相关配置:
[html] view plaincopyprint?
- <!-- 打开Spring的Annotation的支持 -->
- <context:annotation-config />
- <!-- 设定Spring去哪些包中找Annotation -->
- <context:component-scan base-package="com.spring" />
完整的XML配置:
[html] view plaincopyprint?
- <?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>
User实体类:
[java] view plaincopyprint?
- 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;
- }
- }
IUserDao接口:
[java] view plaincopyprint?
- 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);
- }
UserDao类:
[java] view plaincopyprint?
- 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);
- }
- }
IUserService接口:
[java] view plaincopyprint?
- 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);
- }
UserService类:
[java] view plaincopyprint?
- 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=http://www.mamicode.com/"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);
- }
- }
UserAction类:
[java] view plaincopyprint?
- 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);
- }
- }
测试类:
[java] view plaincopyprint?
- 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();
- }
- }
运行结果及项目结果图:
Spring中IOC的Annotation实现
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。