applicationContext.xml
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 无参构造-->
<bean id="student_zhgnsan" class="com.xk.pojo.Student">
<property name="stuName" value="张三"></property>
<property name="sex" value="男"></property>
</bean>
<!-- 使用 p命名 注入数据-->
<bean id="student_xiaoming" class="com.xk.pojo.Student" p:stuName="小明" p:sex="男">
</bean>
<!-- 有参构造-->
<!-- name="stuName" 参数名称-->
<!-- value 设置的数据-->
<!-- index 参数的索引号 从0开始-->
<!-- ref 引用数据 一般是另一个bean-->
<!-- type="java.lang.String" 参数类型-->
<bean id="student_wangwu" class="com.xk.pojo.Student">
<constructor-arg index="0" name="stuName" value="兰溪"></constructor-arg>
<constructor-arg index="1" name="sex" value="女"></constructor-arg>
</bean>
<bean id="Test1" class="com.xk.pojo.TestEntity" >
<!-- 特殊符号-->
<property name="speciaCharacter1">
<value><![CDATA[p&c]]></value>
</property>
<property name="speciaCharacter2" >
<value>p&g</value>
</property>
<!-- <property name="student" ref="student_zhgnsan"></property>-->
<!-- 注入对象
方式一 通过ref属性 引用另一个bean
<property name="student" ref="student_zhgnsan"></property>
方式二 通过内部bean
-->
<property name="student" >
<bean class="com.xk.pojo.Student" >
<property name="sex" value="男"></property>
<property name="stuName" value="张三"></property>
</bean>
</property>
<!-- 注入list-->
<property name="list">
<list>
<value>足球</value>
<value>篮球</value>
</list>
</property>
<!-- 注入array-->
<property name="array">
<list>
<value>足球</value>
<value>篮球</value>
</list>
</property>
<!-- 注入set-->
<property name="set">
<set>
<value>足球</value>
<value>篮球</value>
</set>
</property>
<!-- 注入map-->
<property name="map">
<map>
<entry>
<key>
<value>足球</value>
</key>
<value>football</value>
</entry>
<entry>
<key>
<value>篮球</value>
</key>
<value>basketball</value>
</entry>
</map>
</property>
<!-- 注入prop-->
<property name="props">
<props>
<prop key="足球">football</prop>
</props>
</property>
<!-- 注入""-->
<property name="emptyValue">
<value></value>
</property>
<!-- 注入null-->
<property name="nullValue">
<null/>
</property>
</bean>
<bean id="studentDaoImpl" class="com.xk.dao.impl.StudentDaoImpl"></bean>
<bean id="studentServiceImpl" class="com.xk.service.impl.StudentServiceImpl">
<property name="studentDao" ref="studentDaoImpl"> </property>
</bean>
<!-- 包含增强方法的类 用于AOP-->
<bean id="studentAop" class="com.xk.aop.StudentAop"></bean>
<!-- 配置切面-->
<aop:config>
<!--
定义切点
方法语句:访问修饰符 返回值类型 方法名(参数列表){方法体}
-->
<aop:pointcut id="pointcut" expression="execution(* com.xk.service.impl.StudentServiceImpl.query*(..))"/>
<aop:aspect ref="studentAop">
<!-- 前置增强-->
<aop:before method="before" pointcut-ref="pointcut"></aop:before>
<!-- 最终增强-->
<aop:after method="afterLogger" pointcut-ref="pointcut"></aop:after>
<!-- 环绕增强-->
<aop:around method="aroundLogger" pointcut-ref="pointcut"></aop:around>
<!-- 异常增强-->
<aop:after-throwing method="afterThrowing" pointcut-ref="pointcut"></aop:after-throwing>
<!-- 后置增强-->
<aop:around method="invoke" pointcut-ref="pointcut"></aop:around>
</aop:aspect>
</aop:config>
</beans>
applicationContext2.xml
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 注解:就是一个类 用于代替xml配置框架 需要扫描使用注解的位置-->
<context:component-scan base-package="com.xk"></context:component-scan>
</beans>
StudentAop.java
package com.xk.aop;/*@Time:2021/5/8-18:18*/
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import java.util.Arrays;
public class StudentAop {
private Logger logger=Logger.getLogger(StudentAop.class.getName());
public void before(JoinPoint jp){
logger.info("前置增强"+jp.getSignature()+"方法");
}
public Object invoke(ProceedingJoinPoint jp) throws Throwable {
Object object=jp.proceed();
logger.info("这是后置增强"+object);
return object;
}
public void afterLogger(JoinPoint jp){
logger.info("最终增强"+jp.getSignature().getName()+"方法执行结束");
}
public void afterThrowing(JoinPoint jp){
logger.info("这里是异常增强"+jp.getSignature().getName() + "");
}
public Object aroundLogger(ProceedingJoinPoint jp){
logger.info("这里是环绕增强"+jp.getTarget()+"的"+jp.getSignature().getName()+"方 法.方法参数"+ Arrays.toString(jp.getArgs()));
Object result=null;
try {
result=jp.proceed();
logger.info("环绕增强:调用了"+jp.getTarget()+"的"+jp.getSignature().getName()+"方法。返回值:"+result);
} catch (Throwable throwable) {
logger.info("环绕增强:"+jp.getSignature().getName()+"方法发送异 常"+throwable);
}finally {
logger.info("环绕增强:"+jp.getSignature().getName()+"方法结束执行");
}
return result;
}
}
StudentDaoImpl.java
package com.xk.dao.impl;/*@Time:2021/5/6-13:13*/
import com.xk.dao.StudentDao;
import com.xk.pojo.Student;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository("studentDaoImpl")//用于注解Dao层
public class StudentDaoImpl implements StudentDao {
@Override
public List<Student> queryStudent() {
System.out.println("查询所有学生");
return null;
}
}
StudentServiceImpl.java
package com.xk.service.impl;/*@Time:2021/5/6-13:21*/
import com.xk.dao.StudentDao;
import com.xk.pojo.Student;
import com.xk.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import java.util.List;
@Service("studentServiceImpl") //注解Servic层
public class StudentServiceImpl implements StudentService {
public StudentDao getStudentDao() {
return studentDao;
}
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
@Autowired //按照类型注入
@Qualifier("studentDaoImpl") //按名称来注入
private StudentDao studentDao;
@Override
public List<Student> queryStudent() {
return studentDao.queryStudent() ;
}
}
Student
package com.xk.pojo;/*@Time:2021/5/6-12:41*/
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("student_zhangsan")
public class Student {
@Value("张三")
private String stuName;
@Value("男")
private String sex;
public Student() {
System.out.println("无参构造");
}
public Student(String stuName, String sex) {
this.stuName = stuName;
this.sex = sex;
System.out.println("有参构造");
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
StudentTest.java
package com.xk.test;/*@Time:2021/5/6-12:46*/
import com.xk.pojo.Student;
import com.xk.pojo.TestEntity;
import com.xk.service.StudentService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class StudentTest {
@Test
public void testA(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println("连接成功+++++++++++++++++++++++");
TestEntity testEntity= (TestEntity)applicationContext.getBean("Test1");
testEntity.print();
}
@Test
public void test2() {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext2.xml");
StudentService studentService=(StudentService)applicationContext.getBean("studentServiceImpl");
studentService.queryStudent();
}
@Test
public void test3() {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
StudentService studentService=(StudentService)applicationContext.getBean("studentServiceImpl");
studentService.queryStudent();
}
}
Spring框架配置与AOP实战

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



