简介
Spring是基于IOC
和AOP
的J2EE框架,IOC
:反转控制,传统的通过new 创建对象交由Spring
进行处理,从Spring
中进行获取
核心配置文件
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 通过c获取Category对象,该对象获取的时候注入字符创category 1到name属性中 -->
<bean name="c" class="pojo.Category">
<property name="name" value="category 1" />
</bean>
</beans>
上边重要的是注释部分,通过Spring进行属性的注入
demo
通过spring获取对象,以及通过spring注入的属性
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import pojo.Category;
public class TestSpring {
public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "applicationContext.xml" });
Category c = (Category) context.getBean("c");
System.out.println(c.getName());
}
}
注入对象
<bean name="c" class="pojo.Category">
<property name="name" value="category 1" />
</bean>
<bean name="p" class="pojo.Product">
<property name="name" value="product1" />
<property name="category" ref="c" />
</bean>
注解方式
注入对象,注解方式
在配置文件,中添加<context:annotation-config/>
表示使用注解方式
在相应的对象属性上添加注解@Autowired
或者在相应对象属性setter上添加@Autowired
或者使用@Resource(name="c")
。刚开始学不是很明白,就目前来看个人觉得使用@Resource(name="c")
,@Autowired
如何直到我想使用哪个对象???
全注解方式
在配置文件中使用<context:component-scan base-package="pojo"/>
,其他配置都不要。
在类上添加注解@Component("p")
、@Component("c")
,表示类是bean,另外将属性初始化变为声明时初始化
Spring AOP
面向切面编程,切面就是那些与核心业务无关的代码,比如日志输出、性能统计等。假设想在业务的前后输出日志,准备如下业务类
package service;
//核心业务类
public class ProductService {
public void doSomething(){
System.out.println("dosomething........");
}
}
切面类:
package aspect;
import org.aspectj.lang.ProceedingJoinPoint;
// 日志输出切面类
public class LoggerAspect {
public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
// 切面日志输出
System.out.println("start log:" + joinPoint.getSignature().getName());
// 执行切点方法
Object object = joinPoint.proceed();
// 切面日志输出
System.out.println("end log:" + joinPoint.getSignature().getName());
return object;
}
}
配置文件:
<!-- 业务类 -->
<bean name="s" class="service.ProductService"/>
<!-- 切面类 -->
<bean id="loggerAspect" class="aspect.LoggerAspect"/>
<!-- 将他们编制在一起 -->
<aop:config>
<!-- 切点:执行ProdcutService中不论返回值、所有方法、不论参数类型的方法时使用切面 -->
<aop:pointcut id="loggerCutpoint"
expression=
"execution(* service.ProductService.*(..)) "/>
<!-- 切面 -->
<aop:aspect id="logAspect" ref="loggerAspect">
<!-- 指定切点,调用方法 -->
<aop:around pointcut-ref="loggerCutpoint" method="log"/>
</aop:aspect>
</aop:config>
AOP注解方式
@Aspect
@Component
public class LoggerAspect {
@Around(value ="execution(* service.ProductService.*(..))")
public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
// 切面日志输出
System.out.println("start log:" + joinPoint.getSignature().getName());
// 执行切点方法
Object object = joinPoint.proceed();
// 切面日志输出
System.out.println("end log:" + joinPoint.getSignature().getName());
return object;
}
}
配置文件:
<context:component-scan base-package="aspect"/>
<context:component-scan base-package="service"/>
<aop:aspectj-autoproxy/>
Spring测试
引入jar包
添加注解
// 表示是测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestSpring {
@Autowired
Category c;
@Test
public void test(){
System.out.println(c.getName());
}
}