一、 scope="prototype" 作用域:
singleton 单例,缺省作用域。singleton的默认是在web容器启动时实例化,但也可以配置实例化的lazy延迟,这样就会在第一次被程序调用时实例化。
prototype 访问一次创建一个实例。适用于有状态的Bean,如struts2的action
二、 IOC、Aop
IOC/DI:控制反转/依赖注入:控制权从应用程序转移到框架,由框架来管理对象的创建、销毁的生命周期,依赖关系,实现解耦。
di是实现ioc的一种方式。
主要采用XML/注解+反射+工厂模式。
通过XML配置/注解 JavaBean之间的关系,借助工厂模式进行反射机制来构造对象,管理对象
AOP为Aspect Oriented Programming的缩写,意为:面向切面编程(也叫面向方面)
spring aop通过jdk动态代理实现。
AOP 主要应用于日志记录,性能统计,安全控制 , 事务处理等方面。
三 、SpringMVC
基于REST风格的方式:地址栏url里面可以包含变量,不用使用?后的参数传递
@RequestMapping(value="/user/{id}",method=RequestMethod.GET)
public String show(@PathVariable String id,Model model){
model.addAttribute("user",user对象);
return "user/show";
}
四 spring声名式事务
配置
<!-- Spring的声明式事务管理 -->
<!-- 设置与事务有关的各种属性 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="add*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="update*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="del*" propagation="REQUIRED" rollback-for="Exception" />
<!-- 除了你上面定义的,其他的方法不走事务 -->
<tx:method name="*" read-only="false" />
</tx:attributes>
</tx:advice>
<!-- 声明切入点 -->
<aop:config>
<aop:pointcut id="loginPointCuts"
expression="execution(* com.xx.xx.service.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="loginPointCuts" />
</aop:config>
属性 是否需要? 默认值 描述
name |
是 |
|
与事务属性关联的方法名。通配符(*)可以用来指定一批关联到相同的事务属性的方法。 如:'get*' 、'handle*' 、'on*Event' 等等。 |
propagation |
不 |
REQUIRED |
事务传播行为 |
isolation |
不 |
DEFAULT |
事务隔离级别 |
timeout |
不 |
-1 |
事务超时的时间(以秒为单位) |
read-only |
不 |
false |
事务是否只读? |
rollback-for |
不 |
|
将被触发进行回滚的 Exception(s) ;以逗号分开。 如:'com.foo.MyBusinessException,ServletException' |
no-rollback-for |
不 |
|
不 被触发进行回滚的 Exception(s) ;以逗号分开。 如:'com.foo.MyBusinessException |
关于事务隔离级别,参见我的另一篇博文:http://thrillerzw.iteye.com/blog/1886853
五 单元测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:/applicationContext.xml" })
public class PageServiceTest {
@Autowired
private PageService pageService;
@Test
public void saveOrUpdate() {
PageInfoVo pageInfoVo=new PageInfoVo();
pageInfoVo.setTdPageId(335);
pageService.saveOrUpdate(pageInfoVo);
}
}
import org.junit.runner.RunWith;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:spring/applicationContext.xml"})
@TransactionConfiguration(transactionManager="txManager" ,defaultRollback= true)
@ActiveProfiles("test")
public class SpringTests extends AbstractTransactionalJUnit4SpringContextTests {
}
单元测试直接继承本类,并修改@ContextConfiguration的配置。
默认不提交数据库事务。可以修改@TransactionConfiguration的defaultRollback属性。也可在子类中重新定义@TransactionConfiguration。
补充:
junit3 : 1.单元测试类必须继承自TestCase。 2.要测试的方法必须以test开头。
junit4: :
@Test public 方法,方法名不需要已test开头,如:@Test(expected = IllegalArgumentException.class) 表明当这个方法抛出IllegalArgumentException时测试成功
@Before和@After (使用了该元数据的方法在每个测试方法执行之前都要执行一次。取代了JUnit以前版本中的setUp和tearDown方法)
@lgnore(“该方法还没有实现”)
@BeforeClass @AfterClass 设置在public 静态方法之上,表示在class加载之前执行。这样设置的方法只会执行一次。
六、不能注入时候,获取bean
//ApplicationContextAware 在容器初始化完成后将ApplicationContext注入, 如果你只读ApplicationContext 肯定是线程安全的。
public class SpringContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext;
private SpringContextUtil() {
}
public static Object get(String key) {
if (applicationContext.containsBean(key)) {
return applicationContext.getBean(key);
}
return null;
}
public static Object get(Class cls){
return applicationContext.getBean(cls);
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
@Override
public void setApplicationContext(ApplicationContext cxt)
throws BeansException {
applicationContext = cxt;
}
}
七、监听事件
事件:extends ApplicationEvent
监听者:实现implements ApplicationListener接口,实现了onApplicationEvent(ApplicationEvent event)方法,
主题:通知发生事件。applicationContext 的 publishEvent(ApplicationEvent event)方法,支持基于Observer模式的事件传播机制。
事件传播需要一个事件对象,一个监听者对象。监听者对象一直监听着是否有事件的发生。
代码参考:http://hi.baidu.com/java6666/item/aff06134c0af1a362e0f81ef
八、其它
Spring中有两种类型的Bean,一种是普通Bean,另一种是工厂Bean,即FactoryBean。工厂Bean跟普通Bean不同,其返回的对象不是指定类的一个实例,其返回的是该工厂Bean的getObject方法所返回的对象。
参考:http://tianya23.blog.51cto.com/1081650/660273