依赖
Spring
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.10.RELEASE</version>
</dependency>
控制反转(IoC)
配置文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
使用xml创建对象
数值和字符串
<bean id="hello" class="com.kuang.pojo.Hello">
<property name="name" value="Spring"/>
</bean>
对象
<bean id="world" class="com.*">
<bean id="hello" class="com.kuang.pojo.Hello">
<property name="name" ref="world"/>
</bean>
数组
<bean id="hello" class="com.kuang.pojo.Hello">
<property name="books">
<array>
<value>西游记</value>
<value>红楼梦</value>
<value>水浒传</value>
</array>
</property>
</bean>
List
<bean id="hello" class="com.kuang.pojo.Hello">
<property name="hobbys">
<list>
<value>听歌</value>
<value>看电影</value>
<value>爬山</value>
</list>
</property>
</bean>
Map
<bean id="hello" class="com.kuang.pojo.Hello">
<property name="card">
<map>
<entry key="中国邮政" value="456456456465456"/>
<entry key="建设" value="1456682255511"/>
</map>
</property>
</bean>
Set
<bean id="hello" class="com.kuang.pojo.Hello">
<property name="card">
<map>
<entry key="中国邮政" value="456456456465456"/>
<entry key="建设" value="1456682255511"/>
</map>
</property>
</bean>
有参构造
<bean id="hello" class="com.kuang.pojo.Hello">
<constructor-arg name="name" value="kuangshen2"/>
</bean>
使用自动装配
<beans xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd" >
<context:annotation-config/>
@Autowired
按类型自动匹配,可以使用@Qualifier(value="name")指定装配对象。
@Resource
使用注解开发
<?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
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
</beans>
增加扫描包注解
<context:component-scan base-package="*"/>
@Component(“name”)
使用此命令创建名为name的bean。
还有三个注解等价于此注解。
@Controller@Service@Repository
以上用于springmvc搭建过程。
@Value(“value”)
使用此命令对变量进行注入。
获取Bean
ApplicationContext context = new ClassPathXmlApplicationContext("spring-dao.xml");
JobMapper jobMapper = context.getBean("JobDao", JobMapper.class);
面向切面编程(AOP)
面向切面编程使用代理模式实现。
导入依赖
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
通过Spring API实现
实现以下接口
MethodBeforeAdviceAfterReturningAdvice
xml注册接口
<bean id="log" class="*"/>
<bean id="afterLog" class="*"/>
<aop:config>
<!--切入点 expression:表达式匹配要执行的方法-->
<aop:pointcut id="pointcut" expression="execution(* com.kuang.service.UserServiceImpl.*(..))"/>
<!--执行环绕; advice-ref执行方法 . pointcut-ref切入点-->
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
通过自定义类实现AOP
<bean id="diy" class="com.kuang.config.DiyPointcut"/>
<!--aop的配置-->
<aop:config>
<!--第二种方式:使用AOP的标签实现-->
<aop:aspect ref="diy">
<aop:pointcut id="diyPonitcut" expression="execution(* com.kuang.service.UserServiceImpl.*(..))"/>
<aop:before pointcut-ref="diyPonitcut" method="before"/>
<aop:after pointcut-ref="diyPonitcut" method="after"/>
</aop:aspect>
</aop:config>
通过注解实现
@Aspect
通过此注解将类注册为切面。
@Before(“execution(* com.kuang.service.UserServiceImpl.*(…))”)
设置前置方法。
@After(“execution(* com.kuang.service.UserServiceImpl.*(…))”)
设置后置方法。
@Around(“execution(* com.kuang.service.UserServiceImpl.*(…))”)
设置环绕方法。
本文深入探讨了Spring框架中的控制反转(IoC)和面向切面编程(AOP)概念,包括如何使用XML、自动装配和注解来创建和管理对象,以及AOP的实现方式和相关依赖。
481

被折叠的 条评论
为什么被折叠?



