Spring框架-IOC容器的Bean管理(基于注解方式)

什么是注解:

  • 1.注解是代码特殊标记,格式:@注解名称(属性名称=属性值,属性名称=属性值)
  • 2.使用注解,注解可以使用在类上面,方法上面,属性上面。
  • 3.使用注解目的:简化xml配置。

Spring针对Bean管理重创建对象提供了以下注解:

  • @Component
  • @Service  
  • @Controller
  • @Repository   

上面四个注解功能是一样的,都是可以用来创建bean实例。

1.注解方式实现对象创建的步骤

第一步:引入依赖

第二步:开启组件扫描

设置Context名字空间:

<?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.xsd">
    

</beans>

开启组件扫描:

<!--    开始组件扫描
        如果要扫描多个类的话,可以用逗号进行隔开
        可以扫描上一级的目录
           
        -->
    <context:component-scan base-package="com.annotation.test"></context:component-scan>

第三步:创建类,在类上面添加创建对象注解

package com.annotation.test;

import org.springframework.stereotype.Component;

/**
 * @Description:
 * @Component(value = "userService")与@Component是等价的
 * 因为value值是有默认的
 * 默认值就是类名的首字母小写
 * 并且添加了注解之后,就相当于是bean标签中的<bean id="userService"" class=""></bean>
 *并且Component可以用@Service、@Controller、@Repository替换,所得到的效果相同
 * 
 * @Author:lsy
 * @Date:
 */
@Component(value = "userService")
public class UserService {
    public void add() {
        System.out.println("service add.......");

    }
}

 进行测试:不需要在配置bean就可以得到实例对象

 

2.开启组件扫描的细节问题

3.基于注解方式实现属性的注入

  • 1.@Autowrite:根据属性类型进行自动装配
  • 2.@Qualifier:根据属性名进行注入
  • 3.@Resource:可以根据属性名注入,也可以根据类型注入
  • 4.@value:注入普通类型的属性(例如字符串类型的属性)

前三个是针对对象类型的注入,第四种是针对普通类型进行注入

1.@Autowrite:根据属性类型进行自动装配

第一步:把service和dao对象创建,也就是在service和dao类上面添加创建对象注解

第二步:在service注入dao对象也就是在service类添加dao类型属性,在属性上使用注解

下面的程序中,给dao添加添加注解的时候,是给DAO的实现类添加注解,而不是给接口添加注解。

package com.annotation.test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/**
 * @Description:
 
 * @Author:lsy
 * @Date:
 */
//添加注解创建对象
@Service
public class UserService {
    /*
    定义dao类型的属性
    不需要添加set方法
    添加注入属性的注解,按照类型名自动装配
     */
    @Autowired
    private UserDao userDao;
    public void add() {
        System.out.println("service add.......");

        userDao.add();
    }
}






package com.annotation.test;

import org.springframework.stereotype.Repository;
//添加注解创建对象
@Repository
public class UserDaoImpl implements UserDao {
    @Override
    public void add() {
        System.out.println("dao add.....");
    }
}






    @Test
    public void test2(){
        //1.加载Spring的配置文件
        //文件路径默认在src下。
        ApplicationContext context=new ClassPathXmlApplicationContext("bean5.xml");

        //2.获取配置创建的对象
        UserService userService = context.getBean("userService", UserService.class);
        userService.add();
    }

2.@Qualifier:根据属性名进行注入

这个@Qualifier注解的使用,必须与@Autowrite注解一起使用,否则会注入失败

/*
@Qualifier(value = "userDaoImpl")里面的value值要与@Repository(value ="userDaoImpl")里面的value值相同,要不然会报错

*/

//添加注解创建对象
@Service
public class UserService {
    /*
    定义dao类型的属性
    不与要添加set方法
    添加注入属性的注解,按照类型名自动装配
     */
    @Autowired
    @Qualifier(value = "userDaoImpl")
    private UserDao userDao;
    public void add() {
        System.out.println("service add.......");

        userDao.add();
    }
}



/*
@Qualifier(value = "userDaoImpl")里面的value值要与@Repository(value ="userDaoImpl")里面的value值相同,要不然会报错

*/



package com.annotation.test;

import org.springframework.stereotype.Repository;
//添加注解创建对象
@Repository(value ="userDaoImpl")
public class UserDaoImpl implements UserDao {
    @Override
    public void add() {
        System.out.println("dao add.....");
    }
}

 

3.@Resource:可以根据属性名注入,也可以根据类型注入

特别注意:@Resource​​​​​​​这个注解是Javax包中的,不是Spring包中的,所以一般不进行使用

import javax.annotation.Resource;
//添加注解创建对象
@Service
public class UserService {
    /*
    定义dao类型的属性
    不与要添加set方法
    添加注入属性的注解,按照类型名自动装配
     */
 
//    @Resource  //根据类型进行注入
    @Resource(name="userDaoImpl") //根据名称进行注入
    private UserDao userDao;
    public void add() {
        System.out.println("service add.......");

        userDao.add();
    }
}

4.@value

    @Value(value="abc")   //就可以将abc注入到name的属性值中
    private String name;

4.实现完全注解的开发(一般用SpringBoot进行实现)


import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration  //作为配置类,替代xml配置文件
@ComponentScan(basePackages = {"com.annotation.test"})
//与    <context:component-scan base-package="com.annotation.test"></context:component-scan>
//等价

public class SpringConfig {

}

但是测试代码不同:

package com.annotation.test;

import com.sun.org.apache.xml.internal.res.XMLErrorResources_tr;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @Description:
 * @Author:lsy
 * @Date:
 */
public class SpringConfigTest {
    @Test
    public void test(){
        //1.加载配置类
        ApplicationContext context=new AnnotationConfigApplicationContext(SpringConfig.class);

        UserService userservice=context.getBean("userService",UserService.class);
        userservice.add();
    }

}

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值