spring的注解模式
一、注解中比较常用的方法
- @Component:基本注解,标识了一个受Spring管理的Bean组件
- @Service:标识服务层(业务层)Bean组件
- @Repository:标识持久层Bean组件
- @Controller:标识表现层Bean组件
- 使用注解之前是要实现开启注解的,在配置文件中开启
包扫描的使用
a、简介
- context:component-scan的子元素实现过滤功能
- context:include-filter:表示要包含的目标类
- context:exclude-filter:表示要排除的目标类
类型 | 示例 | 说明 |
---|---|---|
annotation | com.gec.XxxAnnotation | 所有标注了XxxAnnotation的类 |
assignable | com.gec.XxxService | 所有继承或扩展XxxService的类,应类型采用目标类是否继承或扩展了某个特定类进行过滤。 |
aspectj | com.gec.*Service+ | 所有类名以Service结束的类及继承或扩展它们的类 |
regex | com.gec.gec…* | 所有com.gec.anno类包下的类。该类型采用正则表达式根据目标类的类名进行过滤 |
custom | com.gec.XxxTypeFilter | 采用XxxTypeFile代码方式实现过滤规则。 |
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--对包进行扫描-->
<context:component-scan base-package="com.lin"/>
<!--对包进行开启-->
<context:annotation-config/>
</beans>
1、注解下的@Autowired()使用
- 首先 @Autowired() 需要作用在bean 对象中, 假如在实体类中就需要进行赋值就需要使用@Value ()进行内容赋值
- @Autowired() 跟 required 一起使用,就是对异常抛出不显示不出来(实际还好有异常,本质上问题是没有解决)
@Autowired(required = false)
package com.lin.home.domain;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* @Author Lin_Home
* @Date 2020/10/16 9:54
* @Version 1.0
*/
@Component
public class Employee {
private Integer id;
private String name;
private String price;
private User user;
public Employee() {
}
public Employee(Integer id, String name, String price) {
this.id = id;
this.name = name;
this.price = price;
}
@Autowired()
public void setUser(User user) {
this.user = user;
}
@Value("11")
public void setId(Integer id) {
this.id = id;
}
@Value("大老板")
public void setName(String name) {
this.name = name;
}
@Value("2222")
public void setPrice(String price) {
this.price = price;
}
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
", price='" + price + '\'' +
", user=" + user +
'}';
}
}
- 测试类
package com.lin.home.MainDemo;
import com.lin.home.controller.EmployeeController;
import com.lin.home.domain.Employee;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @Author Lin_Home
* @Date 2020/10/16 10:31
* @Version 1.0
*/
public class MainTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("employee.xml");
//EmployeeController employeeController = (EmployeeController) context.getBean("employeeController");
// employeeController.insert();
System.out.println(employeeController);
Employee employee = (Employee) context.getBean("employee");
System.out.println(employee);
}
}
2、@Component使用
- 作用是生成bean 对象,就如在xml的文件配置
package com.lin.home.domain;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* @Author Lin_Home
* @Date 2020/10/16 10:24
* @Version 1.0
*/
@Component
public class User {
private Integer id;
private String name;
@Value("22")
public Integer getId() {
return id;
}
@Value("lin")
public String getName() {
return name;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
3、@Service的使用
package com.lin.home.service.Impl;
import com.lin.home.dao.EmployeeDao;
import com.lin.home.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
* @Author Lin_Home
* @Date 2020/10/16 9:55
* @Version 1.0
*/
@Service
public class EmployeeServiceImpl implements EmployeeService {
private EmployeeDao employeeDao;
@Autowired
@Qualifier(value = "employeeDaoImpl")
//Qualifier 指的是 多个 实现方法都是实现 employeeDao 也是说明不能准确到运行那个
public void setEmployeeDao(EmployeeDao employeeDao) {
this.employeeDao = employeeDao;
}
@Override
public boolean insert() {
return employeeDao.insert();
}
@Override
public boolean update() {
return employeeDao.update();
}
@Override
public boolean delete() {
return employeeDao.delete();
}
@Override
public boolean query() {
return employeeDao.query();
}
}
4、@Repository的使用
package com.lin.home.dao.Impl;
import com.lin.home.dao.EmployeeDao;
import org.springframework.stereotype.Repository;
/**
* @Author Lin_Home
* @Date 2020/10/16 9:56
* @Version 1.0
*/
@Repository
public class EmployeeDaoImpl implements EmployeeDao {
@Override
public boolean insert() {
System.out.println("执行了添加功能");
return true;
}
@Override
public boolean update() {
System.out.println("执行了修改功能");
return true;
}
@Override
public boolean delete() {
System.out.println("执行了删除功能");
return true;
}
@Override
public boolean query() {
System.out.println("执行了查询功能");
return true;
}
}
5、@Controller是的使用
- 现在还是用的spring的注解使用,刚到springmvc 又是一种注
package com.lin.home.controller;
import com.lin.home.domain.Employee;
import com.lin.home.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
/**
* @Author Lin_Home
* @Date 2020/10/16 9:56
* @Version 1.0
*/
@Controller
public class EmployeeController {
private EmployeeService employeeService;
@Autowired
public void setEmployeeService(EmployeeService employeeService) {
this.employeeService = employeeService;
}
public void insert(){
employeeService.insert();
}
}
6、@ Resource的使用 这个是jdk 自带
- 功能类似于**(@Autowired() 跟@ Qualifier)**
- 同时也是减少了注解代码
7、required 属性
- required配置为true(默认),当注入不存在IOC容器的bean对象,则抛出异常。
- required配置为false,当注入不存在IOC容器的bean对象,不会抛出异常。
8、@Qualifier的使用
@Autowired() 跟 Qualifier使用 : 假如在service层中的调用dao层,而dao层的接口,被多个bean 实现,然后就会报错异常
9、@Scope的使用域
作用域,跟在xml 文件下配置的是类似的,也是直接在作用bean中使用
二、Java Config配置bean
- 指的是用java方式配置bean
用java 写一个配置bean: 实际就相当于是自己注册了 StudentConfig.xml
前提是要进行扫描包而这个扫描包 就不需要配置问,直接用注解模式
@ ComponentScan注解扫包
也可以使用使用条件式扫描包:意思是过滤掉 Controller的下的包或者是子包类
@ComponentScan(
basePackages={"com.gec"},
useDefaultFilters=false,
includeFilters=@ComponentScan.Filter(
type= FilterType.ASPECTJ,
pattern={"com.gec.controller.*Controller+"}))
package lin.com.cn;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
/**
* @Author Lin_Home
* @Date 2020/10/16 15:44
* @Version 1.0
*/
@Configuration
@ComponentScan(basePackages = "lin.com.cn")
@Import(StudentConfig2.class)
public class StudentConfig {
@Bean()
public Employee getEmployee(){
Employee employee = new Employee();
employee.setId(55);
employee.setName("大神");
return employee;
}
}
- 对生成的配置中 进行属性值注入 实际相当于是
package lin.com.cn;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
/**
* @Author Lin_Home
* @Date 2020/10/16 15:44
* @Version 1.0
*/
//主要介绍的是实现java的配置而实现不用配置文件
@Component
public class Employee {
private String name;
private Integer id;
@Value("阿强")
public void setName(String name) {
this.name = name;
}
@Value("1")
public void setId(Integer id) {
this.id = id;
}
public Employee() {
}
public Employee(String name, Integer id) {
this.name = name;
this.id = id;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", id=" + id +
'}';
}
}
- 在配置一个多的bean 文件 实际就相当于是自己注册了 StudentConfig2.xml
package lin.com.cn;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Author Lin_Home
* @Date 2020/10/16 16:09
* @Version 1.0
*/
@Configuration
public class StudentConfig2 {
@Bean()
public Employee getEmployee2(){
return new Employee();
}
}
就如太多的配置的文件,需要关系起来
在java的代码中