Spring使用总结

[size=medium]概念
它就像一个粘合剂,在实际项目中,将前后台程序粘合在一起,构建出一个完整的系统。
IOC
反转控制,依赖注入;将原先对象产生的主动权交给了Spring容器,由Spring容器来为我们完成对象的new的过程,并将对象在我们需要时交给我们。
一.依赖注入的方式:
1.setter注入(重要)
<property name="beanOne" ><ref bean="anotherExampleBean"/></property>
<property name="beanTwo" ref="yetAnotherBean"/>
<property name="integerProperty" value="1"/>
注意,getter/setter方法必须写出来

2.构造方法注入(了解)
如果有多个参数,可以指明参数的类型
<bean id="exampleBean" class="examples.ExampleBean">
<constructor-arg type="int" value="7500000"/>
<constructor-arg type="java.lang.String" value="42"/>
</bean>
如果参数是两个类型的,可以为每个参数指明索引
<constructor-arg index="0" ref="userDao" />
<constructor-arg index="1" ref="userDao"/>
3.p命名空间注入(了解)
注意:属性都需要 getter/setter方法
<bean id="zangga" class="entity.Person" p:name="张嘎" p:word="你好">
<bean id="userService" class="service.UserService" p:userDao-ref="userDao" p:userDao2-ref="userDao">
4.方法注入(用得不多)

注入不同数据类型
参见帮助文档中
Aop
面向切面编程
概念:AOP主要实现的目的是针对业务处理过程中的切面进行提取,它所面对的是处理过程中的某个步骤或阶段,以获得逻辑过程中各部分之间低耦合性的隔离效果。
作用:
将日志的记录,事务的处理,性能统计,安全控制等操作从业务逻辑代码中分离出来,在程序执行过程中,通过动态代理的方式织入到逻辑代码中。

1.切面:执行某一系列逻辑代码的过程
2.切入点:是根据方法和类决定在什么地方织入通知的。
3.切入程序:要动态织入的代码


aop实现方式:
方法一:通过spring自带的通知接口来实现AOP
a.前置通知MethodBeforeAdvice
定义一个前置通知的类来实现MethodBeforeAdvice接口,重写before()方法
b.后置通知AfterReturningAdvice接口
定义一个后置通知的类来实现AfterReturningAdvice接口,重写void afterReturning(Object returnValue, Method m, Object[] args, Object target)
throws Throwable方法
c.环绕通知MethodInterceptor
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println("Before: invocation=[" + invocation + "]");
Object rval = invocation.proceed();
System.out.println("Invocation returned");
return rval;
}
d.ThrowsAdvice接口
public void afterThrowing(Method m, Object[] args, Object target, ServletException ex) {
// Do something with all arguments
}
通过ProxyFactoryBean创建AOP的代理进行引入通知

<bean id="person" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="proxyInterfaces"><value>com.mycompany.Person</value></property>
<property name="target">
<bean class="com.mycompany.PersonImpl">
<property name="name"><value>Tony</value></property>
<property name="age"><value>51</value></property>
</bean>
</property>
<property name="interceptorNames">
<list>
<value>myAdvisor</value>
<value>debugInterceptor</value>
</list>
</property>
</bean>
或使用<aop:config>来配置切面也可以,如下
<bean id="userService" class="biz.impl.UserBizImpl"></bean>
<bean id="userBeforeAdvice" class="advice.UserServiceBefore"></bean>
<bean id="afterUserAdvice" class="advice.AfterUserAdvice"></bean>
<bean id="userMethodInterceptor" class="advice.UserMethodInterceptor"></bean>
<bean id="userExceptionAdvice" class="advice.UserExceptionAdvice"></bean>

<aop:config>
<aop:pointcut expression="execution(* biz.impl.*.*(..))" id="pointcut"/>
<aop:advisor advice-ref="userBeforeAdvice" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterUserAdvice" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="userMethodInterceptor" pointcut-ref="pointcut"></aop:advisor>
<aop:advisor advice-ref="userExceptionAdvice" pointcut-ref="pointcut"></aop:advisor>
</aop:config>

方法二.基于Schema的AOP ,使用xml文件配置AOP
步聚:1.新建UserBiz接口,提供方法addUser()方法,delUser()方法
2.新建UserBizImpl实现类,重写addUser(),delUser()方法
3.新建AOP日志记录类 LogAdvice,增加before(),after(),around(),error()方法等

public class LogAdvice {
public void before(){
System.out.println("我在方法之前执行");
}
public void after(){
System.out.println("我在方法之后执行");
}
public Object around(ProceedingJoinPoint pjp) throws Throwable{
System.out.println(pjp.getTarget()+"对象的"+pjp.getSignature().getName()+"方法开始执行");
Object target = pjp.proceed();
System.out.println("环绕通知");
after();
return target;
}
public void error(JoinPoint jp){
System.out.println(jp.getSignature().getName()+"方法出错啦");
}
}
配置日志记录类:<bean id="logAop" class="aop.LogAdvice"></bean>

4.配置切面:
<aop:config>
<aop:pointcut id="pointcut" expression="execution(* biz.impl.*.*(..))" />
<!-- 引用包含增强方法的Bean -->
<aop:aspect ref="logAop">
<!-- 将before()方法定义为前置增强并引用pointcut切入点 -->
<aop:before method="before" pointcut-ref="pointcut"></aop:before>
<!-- 将afterReturning()方法定义为后置增强并引用pointcut切入点 -->
<!-- 通过returning属性指定为名为result的参数注入返回值 -->
<!-- <aop:after-returning method="after" pointcut-ref="pointcut" returning="result"/>-->
<aop:around method="around" pointcut-ref="pointcut" ></aop:around>
</aop:aspect>
</aop:config>
方法三:使用@AspectJ 注解
步聚:
1.启用Spring对@AspectJ的支持,在spring中配置
<aop:aspectj-autoproxy/>
2.配置切面程序的bean:
<bean id="log" class="advice.log.LogAdvice"/>


3.声明一个切面日志记录类
@Aspect
public class LogAdvice {
//前置通知
@Before("execution(* biz.impl.*.*(..))")
public void before(JoinPoint jp){
TimeLimit t = (TimeLimit)Test.cac.getBean("haoShi");
t.setBeginTime(new Date());
System.out.println(jp.getSignature().getName()+"方法之前执行");
}
//后置通知
@AfterReturning("execution(* biz.impl.*.*(..))")
public void after(JoinPoint jp){
TimeLimit t = (TimeLimit)Test.cac.getBean("haoShi");
t.setEndTime(new Date());
System.out.println(jp.getSignature().getName()+"方法执行结束,耗时:"+t.haoShi()+"毫秒");
}
//环绕通知
@Around("execution(* biz.impl.*.*(..))")
public Object around(ProceedingJoinPoint pjp) throws Throwable{
System.out.println(pjp.getTarget()+"对象的"+pjp.getSignature().getName()+"方法开始执行");
Object target = pjp.proceed();
System.out.println("环绕通知");

return target;
}
//异常通知
@AfterThrowing("execution(* biz.impl.*.*(..))")
public void error(JoinPoint jp){
System.out.println(jp.getSignature().getName()+"方法出错啦");
}
}

说明:@Aspect 注解表示切面类
@Before("execution(* biz.impl.*.*(..))")表示前置通知
@AfterReturning(pointcut = "execution(* biz.IUserBiz.*(..))", returning = "returnValue") 表示后置通知
@Around("execution(* biz.impl.*.*(..))") 表示环绕通知
@AfterThrowing("execution(* biz.impl.*.*(..))")表示异常通知

二.生命周期
<bean id="person" class="entity.Person" init-method="init" destroy-method="destroy" scope="singleton">
1.spring容器调用无参构造方法创建对象
2.使用setter方法给属性注入值
3.init()初始化的方法被调用
4.用户手动调用的方法被执行
5.destory方法销毁对象
注意如果bean的 scope="prototype"不会销毁


三.自动装配
<bean id="userService" class="service.UserService" autowire="byType" ></bean>
autowire="byType":按类型进行匹配,如果容器中存在一个与指定属性类型相同的bean,那么将与该属性自动装配。如果存在多个该类型的bean,那么将会抛出异常,并指出不能使用byType方式进行自动装配。若没有找到相匹配的bean,则什么事都不发生,属性也不会被设置
autowire="byName":根据属性名自动装配。此选项将检查容器并根据名字查找与属性完全一致的bean,并将其与属性自动装配

bean的作用域:singleton prototype request session global session


四.注解
作用: 简化xml中bean的配置
步聚:
1.引用命名空间
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
2.在applicationContext.xml中加入注解的配置
<context:annotation-config/>
<!-- 扫描包中注解标注的类 -->
<context:component-scan base-package="dao.impl,service,entity"></context:component-scan>
3.在类上面加入注解
在类上加注解并取名字
@Autowired 写在setter方法上表示自动按名字或类型注入,配合@Qualifier("userDao")来指明要注入的组件名字
例如:
@Autowired
public void setUserDao(@Qualifier("userDao")UserDao userDao) {
this.userDao = userDao;
}


@Component("person") 配在类上 引号中的内容表示给组件取名字,相当于bean标签中的id属性值 <bean id="person">
@Resource(name="userDao")配在属性或字段上,表示给属性进行注入,name表示要注入的组件的名字

@Controller("userService") 配在action的类上
@Service("userService") 配在service或biz的类上
@Repository("userDao") 配在dao包下的类上

@PostConstruct 注解实始化方法
@PreDestroy 注解销毁方法
Spring,Hibernate,Struts框架整合
1.新建web项目,如sshDemo
2.将spring,struts2,hibernate支持的包都拷到项目的lib目录下
3.新建applicationContext.xml,struts.xml,hibernate.cfg.xml三个框架的主配置文件
4.在web.xml中整合spring和struts2
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
5.写用户的注册页,登录页,以及显示用户的首页
6.写UserDaoImpl实现类完成,addUser() ,getAllUser() ,checkUser()等一系列的功能,并抽取UserDao接口
注意:UserDaoImpl 继承HibernateDaoSupport类并实现UserDao接口
写private SessionFactory sessionFactory;属性,getter/setter不要


7.编写UserBizImpl业务类,完成对Dao层方法的调用
写UserDao的属性,private UserDao userDao;提供getter/setter


8.编写UserAction类,提供login()登录的方法,getAllUser()查询所有的方法, reg()注册的方法
写biz属性: private UserBiz userBiz; getter/setter

9.在applicationContext.xml中配置SessionFactory

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
10.在applicationContext.xml中配置Dao,biz,action等,并将dao注入到biz,将biz注入到action
<!-- 将sessionFactory注入到userDao中-->
<bean id="userDao" class="dao.impl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 将Dao注入到Biz中 -->
<bean id="userBiz" class="biz.impl.UserBizImpl">
<property name="userDao" ref="userDao"></property>
</bean>
<!-- 将biz注入到action中-->
<bean id="userAction" class="action.UserAction">
<property name="userBiz" ref="userBiz"></property>
</bean>


11.在struts.xml中配置action,注意,action标签中的class属性与bean中的id属性一样
<action name="userAction" class="userAction">
<result name="success">success.jsp</result>
<result name="input">reg.jsp</result>
</action>

12.配声明事务
<!-- 事务管理器 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 通知 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="find*" read-only="true" />
<tx:method name="search*" read-only="true" />
<tx:method name="query*" read-only="true" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="do*" propagation="REQUIRED" />
<tx:method name="*" propagation="REQUIRED" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 配切面 -->
<aop:config>
<aop:pointcut id="servicePointcut" expression="execution(* biz.impl.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="servicePointcut"/>
</aop:config>
13,数据处理有Spring的模板模式
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
private SessionFactory sessionFactory;

public List<User> checkUser(User user){
return this.getHibernateTemplate().find("from User where username=? and pwd=?", new Object[]{user.getUsername(),user.getPwd()});
}
public void saveUser(User user){
this.getHibernateTemplate().save(user);
}
public List<User> getAllUser(){
return this.getHibernateTemplate().find("from User");
}
/*
public List<User> checkUser(User user){
String hql="from User where username=? and pwd=?";
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
Query query = session.createQuery(hql);
query.setParameter(0, user.getUsername());
query.setParameter(1, user.getPwd());
List<User> userList = query.list();
tx.commit();
return userList;
}
[/size]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值