@Component
public class XXX{}
这个阶段的注解,仅仅是简化XML的配置,并不能完全替代XML
@Component("u")
如果注解与配置同时出现,配置会覆盖注解的配置
@component是spring中的一个注解,它的作用就是实现bean的注入。 在Java的web开发中,提供3个@Component注解衍生注解(功能与@component一样)
衍生注解: 1、@Controller 控制器(注入服务) 用于标注控制层,相当于struts中的action层。 2、@Service 服务(注入dao) 用于标注服务层,主要用来进行业务的逻辑处理。 3、@Repository(实现dao访问) 用于标注数据访问层,也可以说用于标注数据访问组件,即DAO组件。 @Component泛指各种组件,就是说当我们的类不属于各种归类的时候(不属于@Controller、@Services等的时候),我们就可以使用@Component来标注这个类。
<bean id="" class="" scope="singleton|prototype"/>
prototype
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @description:
* @author: shu
* @createDate: 2023/7/29 23:05
* @version: 1.0
*/
public class AplTest {
/**
* @description: 测试组件注解
*/
@Test
public void componentTest(){
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
// 注解方式创建对象时,对象的id默认为类名首字母小写
Object user = applicationContext.getBean("user");
System.out.println(user);
// 通过类型获取对象
Object user1 = applicationContext.getBean("user");
System.out.println(user1);
// // 配置
// Object user1 = applicationContext.getBean("user1");
// System.out.println(user1);
}
}
我们可以看到在原型模式下,他会创建每一个实例,而默认单例实例下全局只有一个实例
<bean id="" class="" lazy="false"/>
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @description:
* @author: shu
* @createDate: 2023/7/29 23:05
* @version: 1.0
*/
public class AplTest {
/**
* @description: 测试组件注解
*/
@Test
public void componentTest() {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
// 注解方式创建对象时,对象的id默认为类名首字母小写
Object user = applicationContext.getBean("user");
System.out.println(user);
// 通过类型获取对象
Object user1 = applicationContext.getBean("user");
System.out.println(user1);
// // 配置
// Object user1 = applicationContext.getBean("user1");
// System.out.println(user1);
}
/**
* 懒加载测试
*/
@Test
public void lazyTest() {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
// 首先获取所有的bean
String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
// 遍历所有的bean
for (String beanDefinitionName : beanDefinitionNames) {
// 获取bean的类型
Class<?> type = applicationContext.getType(beanDefinitionName);
// 输出bean的类型
System.out.println(type);
}
Object user = applicationContext.getBean("user");
System.out.println(user);
}
}
package com.shu.model;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
/**
* @description: 用户类
* @author: shu
* @createDate: 2023/7/29 23:04
* @version: 1.0
*/
@Component
@Lazy
public class User {
private String name;
private int age;
@PostConstruct
public void init() {
System.out.println("初始化方法");
}
@PreDestroy
public void destroy() {
System.out.println("销毁方法");
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
public User() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
/**
* 懒加载测试
*/
@Test
public void lazyTest() {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
// // 首先获取所有的bean
// String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
// // 遍历所有的bean
// for (String beanDefinitionName : beanDefinitionNames) {
// // 获取bean的类型
// Class> type = applicationContext.getType(beanDefinitionName);
// // 输出bean的类型
// System.out.println(type);
// }
Object user = applicationContext.getBean("user");
System.out.println(user);
applicationContext.close();
}
package com.shu.service;
import com.shu.mapper.UserMapper;
import com.shu.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Random;
/**
* @description:
* @author: shu
* @createDate: 2023/7/24 20:10
* @version: 1.0
*/
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
}
/**
* 查询所有用户
*/
public void getUser(){
userMapper.getUserList().forEach(System.out::println);
}
/**
* 新增用户
*/
public void addUser() {
int nextInt = new Random().nextInt(100);
userMapper.addUser(new User(5,"shu04"+nextInt,"123456"));
}
/**
* 修改用户
*/
public void updateUser() {
userMapper.updateUser(new User(4, "sh256", "123456"));
}
}
/**
* 测试 @Autowired
*/
@Test
public void autowiredTest() {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = applicationContext.getBean("userService", UserService.class);
userService.getUser();
}
package com.shu.mapper;
import com.shu.model.User;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* @description:
* @author: shu
* @createDate: 2023/7/24 19:27
* @version: 1.0
*/
@Repository("userMapper01")
public interface UserMapper {
/**
* 查询所有用户
* @return
*/
List<User> getUserList();
/**
* 根据id查询用户
* @param id
* @return
*/
User getUserById(int id);
/**
* 新增用户
*/
void addUser(User user);
/**
* 修改用户
*/
void updateUser(User user);
}
package com.shu.service;
import com.shu.mapper.UserMapper;
import com.shu.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import java.util.Random;
/**
* @description:
* @author: shu
* @createDate: 2023/7/24 20:10
* @version: 1.0
*/
@Service
public class UserService {
@Autowired
@Qualifier("userMapper01")
private UserMapper userMapper;
public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
}
/**
* 查询所有用户
*/
public void getUser(){
userMapper.getUserList().forEach(System.out::println);
}
/**
* 新增用户
*/
public void addUser() {
int nextInt = new Random().nextInt(100);
userMapper.addUser(new User(5,"shu04"+nextInt,"123456"));
}
/**
* 修改用户
*/
public void updateUser() {
userMapper.updateUser(new User(4, "sh256", "123456"));
}
}
Set方法与实例变量
// @Autowired
// @Qualifier("userMapper01")
private UserMapper userMapper;
public UserMapper getUserMapper() {
return userMapper;
}
@Autowired
public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
}
@Resource
@Resource
private UserMapper userMapper;
user.id=1
<context:property-placeholder location="classpath:user.properties" />
package com.shu.service;
import com.shu.mapper.UserMapper;
import com.shu.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Random;
/**
* @description:
* @author: shu
* @createDate: 2023/7/24 20:10
* @version: 1.0
*/
@Service
public class UserService {
@Value("${user.id}")
private int id;
@Resource
private UserMapper userMapper;
/**
* 根据id查询用户
*/
public User getUserById() {
return userMapper.getUserById(id);
}
}
在某些时候我们需要指定排除有些实例,那我们应该咋样来配置
annotation:排除特定的注解 不进⾏扫描
aspectj:切⼊点表达式
包切⼊点: com.shu.bean..*
类切⼊点: *..User
regex:正则表达式
custom:⾃定义排除策略框架底层开发
<context:component-scan base-package="com.shu" annotation-config="true" >
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="aspectj" expression="com.shu.model.User"/>
<context:exclude-filter type="assignable" expression="com.shu.model.User"/>
<context:exclude-filter type="custom" expression="com.shu.model.User"/>
<context:exclude-filter type="regex" expression="c"/>
@ComponentScan(basePackages = "com.shu",
excludeFilters = {@ComponentScan.Filter(type=
FilterType.ANNOTATION,value={Service.class}),
@ComponentScan.Filter(type=
FilterType.ASPECTJ,pattern = "*..User1")})
type = FilterType.ANNOTATION value
.ASSIGNABLE_TYPE value
.ASPECTJ pattern
.REGEX pattern
.CUSTOM value
type:assignable:排除特定的类型 不进⾏扫描
annotation:排除特定的注解 不进⾏扫描
aspectj:切⼊点表达式
包切⼊点: com.baizhiedu.bean..*
类切⼊点: *..User
regex:正则表达式
custom:⾃定义排除策略框架底层开发
<context:component-scan base-package="com.shu" use-defaultfilters="false">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Repository"/>
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Service"/>
context:component-scan>
@ComponentScan(basePackages = "com.shu",
useDefaultFilters = false,
includeFilters = {@ComponentScan.Filter(type=
FilterType.ANNOTATION,value={Service.class})})
type = FilterType.ANNOTATION value
.ASSIGNABLE_TYPE value
.ASPECTJ pattern
.REGEX pattern
.CUSTOM value
什么情况下使⽤注解 什么情况下使⽤配置⽂件?
@Component 替换
总结:自己写的类用注解,不适自己写的类用配置比如:DruidDataSource,SqlSessionFactoryBean
Spring在3.x提供的新的注解,⽤于替换XML配置⽂件。
package com.shu.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @description:
* @author: shu
* @createDate: 2023/7/30 9:18
* @version: 1.0
*/
@Configuration
public class AppConfig {
@Bean
public String hello(){
return "hello";
}
}
/**
* 测试@@Configuration
*
*/
@Test
public void configurationTest() {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Object hello = applicationContext.getBean("hello");
System.out.println(hello);
}
Spring在配置Bean中加⼊了@Configuration注解后,底层就会通过Cglib的代理⽅式,来进 ⾏对象相关的配置、处理
@Component及其衍⽣注解 < @Bean < 配置⽂件bean标签 优先级⾼的配置 覆盖优先级低配置