spring
控制反转(ioc)和面向切面(aop)
一.实现spring,学习spring:
一.依赖注入:(set注入)
1.构造器注入
2.Set方法注入(重点)
依赖:bean对象的创建依赖于容器
注入:bean对象中的所有属性,由容器来注入
3.拓展方式注入:
二.bean的自动装配
自动装配是Spring满足bean依赖的一种方式!
Spring会在上下文中自动寻找,并自动给bean装配属性!
在Spring中有三种装配的方法
1.在xml中显示的装配(已学)
2.在java中显示装配
三.隐式的自动装配bean(重要):
不会的知识:(private 类 类对象:private A a:私有属性是A类的对象)
1.byName自动装配 和 byType自动装配
<bean id="cat" class="com.xulei.pogo.Cat"/>
<bean id="dog" class="com.xulei.pogo.Dog"/>
<!-- 1.这种属于手动装配: <bean id="people" class="com.xulei.pogo.People">-->
<!-- <property name="name" value="许磊"/>-->
<!-- <property name="cat" ref="cat"/>-->
<!-- <property name="dog" ref="dog"/>-->
<!-- </bean>-->
<!--2.autowire:自动装配 :1.ByName的原理:会自动在容器中查找和自己对象在set方法后面的值对应的bean id
满足就会自动装配,2.byType;自动查找和自己对象属性类型相同的bean的id,且byType
可以连 bean id="",都不需要,只需要: <bean class="com.xulei.pogo.Dog"/>-->
<bean id="people" class="com.xulei.pogo.People" autowire="byName" >
<property name="name" value="许磊"/>
</bean>
四.使用注解进行实现自动装配
1.导入约束:
<?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:annotation-config/>
</beans>
2.要使用注解,首先要在bean.xml 中配置注解的支持:<context:annotation-config/>
3.然后在实体类的属性上:@Autowired 注解即可。(使用了@Autowired,我们就不用编写Set方法,前提是配置了在了ioc容器中,且符合byname
科普:public String SetName(@Nullable String name):这个意思就是:@Nullable:name这个字段可以为空,不会报错。
@Autowired
@Qualifier(value="id的值为dog1111 ",这两个注解组合起来用:就实现,就算beans 的id不等于dog这个属性,只要id与注解2中的value相同即可。
也就是说Dog这个类有多个对象了,可以指定不同的Dog对象:如dog1,dog2,dog3,但是id中的值需要和注解2中的value中的值相同。
也可以完成注入
五.@Autowired和@Resource 的区别:都是用来自动装配,且都是作用在属性字段上,@Autowired通过byname的方式实现,@Resource 默认通过byname的实现,如果
找不到名字,则通过byType实现,如果两个都找不到就会报错。
六.Spring 开发中要使用注解开发,必须要在maven中导入aop这个包,依赖里已经包含了这个包
七.使用java的方式配置Spring(Spring-07-JavaConfig)
我们现在要完全不使用Spring的xml配置了,全权交给java来做!
JavaConfig是Spring一个子项目,在Spring4之后,它成为了一个核心功能:
八.Spring 之AOP:代理模式是Spring AOP的底层!(面试高频)
.代理模式:为什么要学习代理模式(二十三种设计模式之一)
1.静态代理:和动态代理:(见代码)
九.基于Spring架构的实现AOP的方法:(AOP-01)
1.使用Spring的API接口:Spring-AOp-01
2.自定义来实现AOP(AOP-02)
.自定义一个类,在beans.xml的配置文件上 写登记bean,让spring托管1.
.然后再配置 AOP 实现 切面配置:
<bean id="userservice" class="com.xulei.service.UserServiceImpl"/>
<!--方法二:自定义的类:这两个方法该怎么切入:-->
<bean id="diy" class="com.xulei.diy.Diy"/>
<aop:config>
<!--自定义的类,用aspect:切面这个标签:ref:要引用这个类-->
<aop:aspect ref="diy" >
<!--切入点-->
<aop:pointcut id="pointcut" expression="execution(* com.xulei.service.UserServiceImpl.*(..))"/>
<!--通知:也就是确定具体切入的前还是后(这里是前),用哪个方法切入:before方法-->
<aop:before method="before" pointcut-ref="pointcut"/>
<!--通知:也就是确定具体切入的前还是后(这里是后),用哪个方法切入:after方法-->
<aop:after method="after" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>
.最后编写一个测试类,调用:、、、、基本固定格式:spring的测试类: 这是在xml上配置最后实现spring托管的,所有就是new ClassPathXMlApplicationContext
public class Mytest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userservice = (UserService) context.getBean("userservice");
userservice.pay();
}
}
如果完全用java语言中配置:就是这样调用:new AnnotationCOnfigAppLicationContext:使用注解配置,无需beans.xml配置,让Spring托管
public class mytest {
public static void main(String[] args) {
//如果完全使用了配置类方法去做,我们就只能通过AnnotationConfig 上下文来获取容器,通过配置类的Class对象加载
//通过注解获得上下文,方法后面的参数是:配置类的名称.class。
ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
User getUser = (User) context.getBean("getUser");
System.out.println(getUser.getName());
User getHobby = context.getBean("getHobby",User.class);
System.out.println(getHobby.getHobby());
}
}
3.使用注解来代替beans.xml中对自定义类的配置:(其他不变)
第一步:
@Aspect//标注这个类是一个切面
public class AnnptationPointCut {
//execution:找到这个包下的类的方法,(..):代表无论多少个参数
@Before("execution(* com.xulei.Service.UserServiceImpl.*(..))")
public void before(){
System.out.println("==========请扫描二维码进行支付===========");
}
@After("execution(* com.xulei.Service.UserServiceImpl.*(..))")
public void after(){
System.out.println("==========支付完成===============");
}
}
第二步:
xml中配置一下:在aop中实现注解的配置:就一句话
<!--开启注解支持(AOP中的)-->
<aop:aspectj-autoproxy/>
第三步:测试
public class Mytest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userservice = (UserService) context.getBean("userservice");
userservice.pay();
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)