Spring--IOC容器——对象依赖关系(注解)

注解方式可以简化spring的IOC容器的配置!

使用注解步骤:

  • 1)先引入context名称空间
    xmlns:context="http://www.springframework.org/schema/context"
  • 2)开启注解扫描
  • 3)使用注解
    通过注解的方式,把对象加入ioc容器。
    创建对象以及处理对象依赖关系,相关的注解:
    @Component指定把一个对象加入IOC容器
    @Repository作用同@Component; 在持久层使用
    @Service 作用同@Component; 在业务逻辑层使用
    @Controller作用同@Component; 在控制层使用
    @Resource属性注入

总结:
1) 使用注解,可以简化配置,且可以把对象加入IOC容器,及处理依赖关系(DI)
2) 注解可以和XML配置一起使用。

方法一

注解:





    


UserDao.java

package com.huan.BeanTest;

import org.springframework.stereotype.Component;

/**
 * Created by 马欢欢 on 2017/6/24.
 */
@Component("userDao") //相当于 bean.xml[]
public class UserDao {
    public void save(){
        System.out.println("db:操作数据库");
    }
}

UserService.java

package com.huan.BeanTest;

import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * Created by 马欢欢 on 2017/6/24.
 */
@Component("userService")//userService对象,注入到ioc容器
public class UserService {
    //从IOC容器中找到userDao对象,注入到当前字段
    /**
     * 
     *   相当于 @Resource(name = "userDao")
     *
     *  
     */
    @Resource(name = "userDao")
    private UserDao userDao;
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
    public void save(){
        userDao.save();
    }
}

UserAction.java

package com.huan.BeanTest;

import org.springframework.stereotype.Component;


import javax.annotation.Resource;

/**
 * Created by 马欢欢 on 2017/6/24.
 */
@Component("userAction")
public class UserAction {
    @Resource(name = "userService")
    private UserService userService;
    public void execute(){
        userService.save();
    }
}


创建user对象

 @Test
    public void test2(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("Spring/applicationContext.xml");
        UserAction userAction = (UserAction) applicationContext.getBean("userAction");
        userAction.execute();
Spring--IOC容器——对象依赖关系(注解)_第1张图片
方法二

注解:





    


UserDao.java

package com.huan.BeanTest;

import org.springframework.stereotype.Component;

/**
 * Created by 马欢欢 on 2017/6/24.
 */
@Component
public class UserDao {
    public void save(){
        System.out.println("db:操作数据库zui");
    }
}


UserService.java


package com.huan.BeanTest;

import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * Created by 马欢欢 on 2017/6/24.
 */
@Component
public class UserService {
    
    @Resource//根据类型查找·[在容器中,该类型只有一个变量]
    private UserDao userDao;
    public void save(){
        userDao.save();
    }
}

UserAction.java

package com.huan.BeanTest;

import org.springframework.stereotype.Component;


import javax.annotation.Resource;

/**
 * Created by 马欢欢 on 2017/6/24.
 */
@Component
public class UserAction {
    @Resource
    private UserService userService;
    public void execute(){
        userService.save();
    }
}

创建user对象

 @Test
    public void test2(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("Spring/applicationContext.xml");
        UserAction userAction = (UserAction) applicationContext.getBean("userAction");
        userAction.execute();
Spring--IOC容器——对象依赖关系(注解)_第2张图片
方法三

注解:





    


UserDao.java

package com.huan.BeanTest;

import org.springframework.stereotype.Repository;

/**
 * Created by 马欢欢 on 2017/6/24.
 */
@Repository
public class UserDao {
    public void save(){
        System.out.println("db:操作数据库zui");
    }
}



UserService.java


package com.huan.BeanTest;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Created by 马欢欢 on 2017/6/24.
 */
@Service
public class UserService {

    @Resource
    private UserDao userDa;
    public void save(){
        userDa.save();
    }
}

UserAction.java

package com.huan.BeanTest;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;

/**
 * Created by 马欢欢 on 2017/6/24.
 */
@Controller
public class UserAction {
    @Resource
    private UserService userService;
    public void execute(){
        userService.save();
    }
}

创建user对象

 @Test
    public void test2(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("Spring/applicationContext.xml");
        UserAction userAction = (UserAction) applicationContext.getBean("userAction");
        userAction.execute();
Spring--IOC容器——对象依赖关系(注解)_第3张图片
image.png

上一篇:Spring--IOC容器——对象依赖关系(构造函数、set方法、p名称空间、自动装配)

文集:Spring框架学习

你可能感兴趣的:(Spring--IOC容器——对象依赖关系(注解))