IOC管理Bean--基于注解方式

1、什么是注解?

(1)注解是代码特殊标记,

格式:@注解名称(属性名称=属性值…)

(2)使用注解,注解作用在类上边、方法上边、属性上边

(3)使用注解目的:简化xml配置

2、Spring对Bean管理中创建对象提供的注解

(1)@Component:都可以加

(2)@Service:一般加在service层

(3)@Controller:一般加在controller层

(4)@Repository:一般加在dao层

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

3、基于注解方式实现对象创建

(1)引入依赖

在这里插入图片描述

(2)开启组件扫描

变化图

在这里插入图片描述

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"
       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">

    <!--开启组件扫描
    1、如果扫描多个包,多个包使用逗号分开
    2、扫描包上层目录
    -->
    <!-- 1、逗号分开-->
    <context:component-scan base-package="com.lu.dao,com.lu.service"></context:component-scan>

    <!-- 2、扫描上层-->
    <context:component-scan base-package="com.lu"></context:component-scan>

</beans>

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

package com.lu.service;

import org.springframework.stereotype.Service;

/**
 * 注解里面的value属性值可以不写,
 * 默认是类名称,首字母小写 UserService -- userService
 */

@Service(value = "userService") //相当于xml中的<bean id="userService" class="...">
public class UserService {
    
    
    public void add(){
    
    
        System.out.println("service add...");
    }
}

第四步:测试类Test

import com.lu.service.UserService;
import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    
    
    public static void main(String[] args) {
    
    
        ApplicationContext context =
                new ClassPathXmlApplicationContext("beans.xml");

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

        System.out.println(userService);
        userService.add();
    }
}

4、开启逐渐扫描细节配置

<!--示例 1
use-default-filters="false" 表示不使用默认filter,自己配置
context:include-filter:设置扫描哪些内容。
下边代码是只扫描 Servic注解,其他注解不扫描-->
<context:component-scan base-package="com.lu" use-default-filters="false">
    <context:include-filter type="annotation"
                            expression="org.springframework.stereotype.Service"/>
</context:component-scan>

<!--示例 2
    下面配置扫描包所有内容
    context:component-scan:设置那些内容不扫描。
    下边代码是不扫描 Servic注解,其他注解都扫描
    -->
<context:component-scan base-package="com.lu" >
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>

5、基于注解方式实现属性注入

注解有:

@Autowired:根据属性类型进行自动装配

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

@Resource:可以根据类型注解,可以根据名称注入

@Value:注入普通类型属性

(1)@Autowired:根据属性类型进行自动装配

第一步:创建service和dao对象,在service和dao类添加创建对象注解

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

@Autowired是根据类型注入。现在类型是UserDao,根据这个类型找到UserDao的实现类UserDaoImpl,然后注入。

代码如下:

UserDao类:

package com.lu.dao;

public interface UserDao {
    
    
    public void add();
}

UserDaoImpl类:

package com.lu.dao;

import org.springframework.stereotype.Repository;

@Repository
public class UserDaoImpl implements UserDao {
    
    

    @Override
    public void add() {
    
    
        System.out.println("userDao add.....");
    }
}

UserService类:

package com.lu.service;

import com.lu.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service(value = "userService") //相当于xml中的<bean id="userService" class="...">
public class UserService {
    
    

    //定义dao类型属性
    //不需要添加set方法
    //添加注入属性注解:@Autowired
    @Autowired
    private UserDao userDao;

    public void add(){
    
    
        userDao.add();
    }
}

配置文件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"
       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">
    
    <!-- 开启扫描-->
    <context:component-scan base-package="com.lu"></context:component-scan>

</beans>

测试类 Test:

import com.lu.service.UserService;
import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class HelloTest {
    
    
    public static void main(String[] args) {
    
    
        ApplicationContext context =
                new ClassPathXmlApplicationContext("beans.xml");

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

        System.out.println(userService);
        userService.add();
    }
}

(2)@Qualifier:根据属性名称进行注入

这个@Qualifier注解的使用,要和注解@Autowired一起使用。代码改变的地方:

UserDaoImpl类中:

本来在类上的注解是:@Repository

现在改成:@Repository(value = “userDaoImpl1”)

UserService类中添加如下:

@Autowired
@Qualifier(value = "userDaoImpl1")
private UserDao userDao;

注意:根据类型注入,会有问题。比如UserDao有多个实现类,就不知道找哪个了,这时候就需要根据名称注解了。

(3)@Resource:可以根据类型注解,可以根据名称注入

@Resource,默认按照名称进行装配,名称可以通过name属性进行指定,

如果没有指定name属性,当注解写在字段上时,默认取字段名进行按照名称查找,如果注解写在setter方法上默认取属性名进行装配。

当找不到与名称匹配的bean时才按照类型进行装配。但是需要注意的是,如果name属性一旦指定,就只会按照名称进行装配。

UserDaoImpl类中:

本来在类上的注解是:@Repository

现在改成:@Repository(value = “userDaoImpl1”)

UserService类中:

@Resource(name = "userDaoImpl1") //根据名称注入
//@Resource //加在类型上边,根据什么类型注入
private UserDao userDao;

注意:官方不建议使用这个注解,因为这个注解是J2EE的,使用时导包如下。

import javax.annotation.Resource;
import javax.naming.Name;

(4)@Value:注入普通类型属性

@Value(value = "abc")
private String name;

6、完全注解开发

(1)创建配置类,替代xml配置文件

配置类名字可以随便起:

package com.lu.config;

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

@Configuration //作为配置类,替代xml配置文件
@ComponentScan(basePackages = {
    
    "com.lu"}) //开启包扫描
public class SpringConfig {
    
    
}

(2)测试类中不同的只有加载配置文件的地方:

//        之前
//        ApplicationContext context =
//                new ClassPathXmlApplicationContext("beans.xml");

        //现在
        ApplicationContext context =
                new AnnotationConfigApplicationContext(SpringConfig.class);

练习代码:

练习代码:
链接:https://pan.baidu.com/s/1BOfdPSPSjOmsLYW8cibRBQ
提取码:3fop

猜你喜欢

转载自blog.csdn.net/qq_42524288/article/details/108920430