Spring学习总结:六:基于JavaConfig

一、基于JavaConfig

1. @Configuration :类级别的注解,表示当前类是Java Config配置类,作用是替代XML配置文件

2. @Bean :方法级别的注解,表示方法返回的对象将被SpringIOC容器管理,Bean的id默认为方法名

/**
* @description 基于java代码配置IOC容器
*/
@Configuration
public class IOCConfig {
// <bean id="lili" class="com.softeem.entity.Woman">
// <property name="name" value="莉莉"/>
// <property name="age" value="18"/>
// <property name="outlook" value="面容姣好的"/>
// </bean>
@Bean
public Woman lili(){
Woman woman = new Woman();
woman.setName("莉莉");
woman.setAge(18);
woman.setOutlook("面容姣好的");
return woman;
}
// <bean id="peter" class="com.softeem.entity.Man">
// <property name="name" value="皮特李"/>
// <property name="fund" value="数十亿"/>
// <property name="woman" ref="lili"/>
// </bean>
@Bean
public Man peter(Woman lili){
Man man = new Man();
man.setName("皮特李");
man.setFund("数十亿");
man.setWoman(lili);
return man;
}
}

1. 基于JavaConfig构建IOC容器补充

2. AOP 1. AOP的概念

2. 基于XML配置

3. 基于注解配置

4. JDK动态代理和CGLIB动态代理

二、基于JavaConfig(补充)

@Configuration
@ComponentScan(basePackages = {"com.softeem.dao","com.softeem.service"})
public class IOCConfig {
@Bean
@Scope("prototype")
public Woman lili(){
Woman woman = new Woman();
woman.setName("莉莉");
woman.setAge(18);
woman.setOutlook("面容姣好的");
return woman;
}
@Bean
public Man peter(Woman lili){
Man man = new Man();
man.setName("皮特李");
man.setFund("数十亿");
man.setWoman(lili);
return man;
}
@Bean
@Primary
public Man jack(Woman lili){
Man man = new Man();
man.setName("杰克马");
man.setFund("数百亿");
man.setWoman(lili);
return man;
}
}
public class App2 {
public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext = new
AnnotationConfigApplicationContext();
applicationContext.register(IOCConfig.class);
applicationContext.register(IOCConfig2.class);
//无参情况下,需要手动刷新,但是仅能刷新一次
applicationContext.refresh();
String[] beanNames = applicationContext.getBeanDefinitionNames();
for (String beanName : beanNames) {
System.out.println(beanName);
}
System.out.println("=========");
}

三、.Spring AOP

1、AOP(面向切面编程)是OOP的延续,SpringAOP是基于AspectJ实现 类与方法 的匹配,但是SpringAOP 底层是基于代理实现的对对象运行时的功能扩展 

2、AOP的术语

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.6</version>
</dependency>

 

/**
* @author muzi@softeem.com
* @description 切面
* @since 2021/4/8 21:20
*/
public class MyAspect {
/**
* 切面的方法用于扩展功能
* 记录执行事件
* joinPoint :连接点
*/
public void logExecuteTime(JoinPoint joinPoint){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String current = sdf.format(new Date());
System.out.println("当前时间为:" + current);
//getTarget()获取目标对象
Object target = joinPoint.getTarget();
System.out.println("目标对象:"+target);
//getSignature() 获取目标方法
Signature signature = joinPoint.getSignature();
System.out.println("目标方法:"+signature);
Object[] args = joinPoint.getArgs();
System.out.println("目标方法有"+args.length+"个参数");
for (Object arg : args) {
System.out.println("参数值为:" + arg);
}
}
}
public class UserService {
public void insertUser(){
System.out.println("UserService创建用户");
}
public void queryUser(String id){
System.out.println("UserService查询到id为"+id+"的用户");
}
}
<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="userService" class="com.softeem.service.UserService"/>
<!-- 注册Bean-->
<bean id="myAspect" class="com.softeem.aspect.MyAspect" />
<!-- AOP的配置 -->
<aop:config>
<!-- public void insertUser(){
System.out.println("UserService创建用户");
} -->
<!-- 配置切入点 ,需要配合切点表达式说明 -->
<!-- "execution(public * com.softeem..*.*(..))"代表切面作用在com.softeem包
及其子包下的所有类的所有方法上 -->
<aop:pointcut id="firstPC" expression="execution(public *
com.softeem..*.*(..))"/>
<!-- 定义切面 -->
<aop:aspect ref="myAspect">
<aop:before method="logExecuteTime" pointcut-ref="firstPC" />
</aop:aspect>
</aop:config>
</beans>
public class App {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new
ClassPathXmlApplicationContext("classpath:applicationContext.xml");
//
UserService userService = context.getBean("userService",
UserService.class);
userService.insertUser();
userService.queryUser("100");
}
}

4、总结

1. 切点 和 切点表达式 定位到需要被增强的方法上;

2. 然后通过 切面 中的 连接点 方法对需要被增强的方法进行增强;

3. 具体增强在哪里,通过 通知 的类型来确定

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值