Spring_管理bean对象 - - 基于注解

推荐视频:尚硅谷-Spring5框架最新版教程(idea版)

这是我觉得很不错的一套视频,我的spring系列的文章全是学习上述视频的学习笔记

管理bean对象 - - - 基于注解

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

注解的位置:类、方法、参数

使用格式:@注解名(属性名=属性值,属性名=属性值)

1 基于注解创建对象

4个创建对象的注解

  • component
  • controller
  • service
  • repository

1.1 导入依赖 - - - aop


<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-aopartifactId>
    <version>5.2.9.RELEASEversion>
dependency>

1.2 配置文件

如果要自动扫描一个包(com.xtc)下的所有类,可以通过下列方式开启组件扫描

如果要扫描"com.xtc.a"包和"com.xtc.b"包,有两个方法,


<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.xtc"/>
beans>

1.3 基类

@Componet 表示,默认创建对象名为department,及类名的小驼峰命名

相当于:@Component(value=“department”)

相当于:

import org.springframework.stereotype.Component;

@Component
public class Department {
    @Override
    public String toString() {
        return super.toString();
    }
}

1.4 测试类

public class DepartmentTest {

    @Test
    public void test01(){
        ApplicationContext context =
               new  ClassPathXmlApplicationContext("bean8.xml");
        Department department = context.getBean("department", Department.class);
        System.out.println(department.toString());
    }

}

2 两种常见的包扫描策略

2.1 扫描包下,被component注解标注的类

<context:component-scan base-package="com.xtc.entity" use-default-filters="false">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Component"/>
context:component-scan>

2.2 不扫描包下,被component注解标注的类

<context:component-scan base-package="com.xtc.entity" use-default-filters="false">
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Component"/>
context:component-scan>

3 基于注解注入属性

三个注入对象的注解:

① @Autowired,按照类型注入

② @Qualifier,按照名称注入,并且与@Autowired一起使用

③ @Resource,安装名称或属性注入。先按照???

一个注入普通属性的注解:

@Value

3.1 @Autowired

基类:UserService,UserServiceImpl,UserDao,UserDaoImpl

package com.xtc.service;

public interface UserService {
    void getImpl();

}
package com.xtc.service.impl;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;


    public void getImpl(){
        userDao.getImplName();
    }
}
package com.xtc.dao;

public interface UserDao {
    void getImplName();
}

package com.xtc.dao.impl;

@Repository
public class UserDaoImpl implements UserDao {
    public void getImplName() {
        System.out.println(" UserDaoImpl");
    }
}

配置文件


<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.xtc"/>
beans>

测试方法

@Test
public void test02(){
    ApplicationContext context =
    	new ClassPathXmlApplicationContext("bean9.xml");
    UserService userService = context.getBean("userServiceImpl", UserServiceImpl.class);
    userService.getImpl();
}

3.2 @Qualifier

  • 如果一个接口有多个实现类,那么按类型注入时,可能会因为不知道注入哪个而出错,这是我们要配合Queifier注解,按名称进行注入。来确定唯一的注入对象。

基类,在7.3.1 的基类的基础上,添加一个UserDao的实现类,UserMysqlImpl

package com.xtc.service;

public interface UserService {
    void getImpl();

}
package com.xtc.service.impl;

@Service
public class UserServiceImpl implements UserService {

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


    public void getImpl(){
        userDao.getImplName();
    }
}
package com.xtc.dao;

public interface UserDao {
    void getImplName();
}

package com.xtc.dao.impl;

@Repository
public class UserDaoImpl implements UserDao {
    public void getImplName() {
        System.out.println(" UserDaoImpl");
    }
}
@Repository
public class UserMysqlImpl implements UserDao {
    public void getImplName() {
        System.out.println("UserMysqlImpl");
    }
}

3.3 @Resource

@Resource:按照类型注入

@Resource(value=“className”):按照名称注入

3.4 @Value

@Value(value="王大锤")
private String username;

4 全注解开发

@Configuration					
	相当于:无其他配置的bean.xml
@ComponentScan(basePackages = "com.xtc.entity")		
	开启包扫描,相当于 

4.1 基类

@Component
public class People {
    private String name;

    public void abc() {
        System.out.println("abc");
    }
}

4.2 配置类

@Configuration
@ComponentScan(basePackages = "com.xtc.entity")
public class SpringConfig {
}

4.3 测试类

public class PeopleTest {
    @Test
    public void test01(){
        ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
        People people = context.getBean("people", People.class);
        people.abc();
    }

}

你可能感兴趣的:(java,后端框架,spring,java)