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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd "> --> <!-- 定义UserDao对象,并指定id为userDao --> <!-- <bean id="userDao" class="dao.impl.UserDao" /> 定义UserBiz对象,并指定id为userBiz <bean id="userBiz" class="biz.impl.UserBiz"> 为userBiz的dao属性赋值,需要注意的是,这里要调用setDao()方法 <property name="dao"> 引用id为userDao的对象为userBiz的dao属性赋值 <ref bean="userDao" /> <bean class="dao.impl.UserDao"></bean> </property> <property name="num"> <value>10</value> </property> </bean> --> <!-- <bean id="userDao1" class="com.demo.dao.UserDaoImpl"></bean> <bean id="userService" class="service.UserServiceImpl"> <property name="userDao"> <ref bean="userDao1"/> </property> </bean> --> <!-- <bean id="userDao" class="com.demo.dao.UserDaoImpl"></bean> <bean id="userService" class="service.UserServiceImpl"> <constructor-arg> <ref bean ="userDao"/> </constructor-arg> </bean> --> <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 http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"> <bean id="userService" class="com.aop.service.UserService"/> <bean id="servicelogging" class="com.aop.service.ServiceLogging"/> <aop:config> <aop:pointcut expression="execution(public * com.aop.service.*.*Service(..))" id="servicePointcut"/> <aop:aspect ref="servicelogging"> <aop:before method="before" pointcut-ref="servicePointcut"/> <aop:after method="after" pointcut-ref="servicePointcut"/> <aop:after-returning method="afterReturing" pointcut-ref="servicePointcut"/> <aop:after-throwing method="afterThrowing" pointcut-ref="servicePointcut"/> <aop:around method="around" pointcut-ref="servicePointcut"/> </aop:aspect> </aop:config> </beans>
User.java
package com.aop.entity; public class User { private int id; private String username; private String password; private String email; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public User(int id, String username, String password, String email) { super(); this.id = id; this.username = username; this.password = password; this.email = email; } public User() { super(); // TODO Auto-generated constructor stub } }
UserService
package com.aop.service; import com.aop.entity.User; public class UserService { /** * @param args */ public void addUserService(User user){ System.out.println("业务方法被执行"); System.out.println(user.getUsername()); } }
ServiceLogging
package com.aop.service; import org.aspectj.lang.ProceedingJoinPoint; public class ServiceLogging { public void before(){ System.out.println("前置增强处理被执行"); } public void after(){ System.out.println("最终增强处理被执行"); } public void afterReturing(){ System.out.println("后置增强处理被执行"); } public void afterThrowing(){ System.out.println("抛异常增强处理被执行"); } public void around(ProceedingJoinPoint pjp){ System.out.println("环绕前置增强处理被执行"); try { pjp.proceed(); } catch (Throwable e) { e.printStackTrace(); } System.out.println("环绕后置增强处理被执行"); } }
AopTest.java
package com.aop.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.aop.entity.User; import com.aop.service.UserService; public class AopTest { /** * @param args */ public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); UserService service=(UserService)ctx.getBean("userService"); User user = new User(10,"Tom","123456","504177380@qq.com"); service.addUserService(user); } }
Spring的本质是什么?
1.对象的创建
new 抽象类 工厂模式
工厂模式,以及其他模式像抽象工厂,
Builder模式提供的都是创建对象的方法。
这背后体现的都是“封装变化”的思想。
这些模式只是一些最佳实践而已: 起了一个名称、描述一下解决的问题、使用的范围和场景,在项目中还得自己去编码实现他们。
2.解除依赖
面向接口编程
3.Spring依赖注入
在Java 世界里,如果想描述各种逻辑关系, XML是不二之选
这个xml 挺容易理解的, 但是仅仅有它还不够, 还缺一个解析器(假设叫做XmlAppContext)来解析,处理这个文件,
基本过程是:0. 解析xml, 获取各种元素
1. 通过Java反射把各个bean 的实例创建起来:
2. 还是通过Java反射调用 的两个方法:
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService service=(UserService)ctx.getBean("userService");
4.IOC VS DI