配置bean的方式(两种):
1. xml配置
2. 注解的方式
1,注解的方式
@Repository dao层 持久层
默认的bean的id为类名首字母小写:
如果我们要自己制定bean的id ?
@Repository(“aa”)这样相当于如下代码
@Service biz层 业务层
@Control和@Scope("prototype")配合使用 web层
@Component 所有层都可以用
@Autowired 自动注入 按类型
@Qualifier("aa") 按名称 按名称要两个配合使用
总结:bean的配置是用注解还是用xml配置?
答:配合使用
例子:
在持久层写了@Repository这个注解就相当于在配置文件中添加了如下代码:
< bean id="treeDaoImpl" class="com.kz.dao.impl.TreeDaoImpl">< /bean>默认情况下id是类名的首字母小写。(其它注解也是同样的注入方式)
@Repository
public class TreeDaoImpl implements TreeDao{
public int save(Tree tree) {
Session session = HibernateSessionFactory.getSession();
int num=(Integer)session.save(tree);
return num;
}
}
@Service
public class TreeBizImpl implements TreeBiz {
//如果在属性上注解写是@Autowired,那么就是自动注入,按类型注入,
//也就是在
<bean id="treeBizImpl" class="com.kz.biz.impl.TreeBizImpl">
//中有如下这个属性代码,name为TreeBizImpl类里你所写的属性名**treedao**(下面打了** **的那个名字),ref为TreeDao类型中的实现类的类名小写。
<property name="treedao" ref="treeDaoImpl"></property>
</bean>
//如果在属性上注解写的是@Autowired加@Qualifier("treeDaoImpl22"),那么就是按id注入,也就是上面的ref为你所写的treeDaoImpl22名字,也就是说treeDao的那个bean的id为你所写的treeDaoImpl22名字。
@Autowired
@Qualifier("treeDaoImpl22")
private TreeDao **treedao**;
public int save(Tree tree) {
int num=treedao.save(tree);
return num;
}
}
@Controller
@Scope("prototype")
public class TreeAction extends ActionSupport{
private Tree tree;
public TreeAction() {
System.out.println("创建了TreeAction对象");
}
@Autowired
@Qualifier("treeProxy")
private TreeBiz treebiz;
public String add(){
int num=treebiz.save(tree);
if(num>0){
return SUCCESS;
}else{
return ERROR;
}
}
}
@Component("transactionProxy")
public class TransactionProxy implements MethodInterceptor{
public Object invoke(MethodInvocation invocation) throws Throwable {
//在目标方法前添加事务
System.out.println("session事务开启了");
Session session = HibernateSessionFactory.getSession();
session.beginTransaction();
//如果在调用目标方法的时候出现异常就要进行异常捕获
try {
//调用目标方法
int num = (Integer)invocation.proceed();
System.out.println("session事务提交了");
//在目标方法之后提交事务
session.beginTransaction().commit();
return num;
} catch (Exception e) {
//如果有异常就要回滚
System.out.println("session事务回滚了");
session.beginTransaction().rollback();
}finally{
//最后才是关闭session
System.out.println("session事务关闭了");
HibernateSessionFactory.closeSession();
}
//如果错误就返回null
return null;
}
}
spring+struts2
action怎么交由bean工厂来创建?
1,在applicationCotext.xml配置action(可以用 xml注入 或者 注解注入,上面的方式就是注解注入)
2,在struts.xml中class改为该action在bean工厂中对应的id
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="kzdefault" namespace="/" extends="struts-default">
<action name="tree_*" class="treeAction" method="{1}">
<result>/index.jsp</result>
</action>
</package>
</struts>
3,加入一个包:spring-struts-plugin.jar的spring与struts2的集成包
4,在web.xml中加入spring的配置:
bean工厂也要交给web容器来初始化(读取配置文件)
<!-- spring的核心配置 -->
<!-- 监听器默认加载的是WEB-INF/applicationContext.xml -->
<listener>
<listener-
class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 指定Spring框架的配置文件所在的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext1.xml</param-value>
</context-param>
spring的单元测试
//spring也有单元测试,使用时需要加如下注解
//开始运行
@RunWith(SpringJUnit4ClassRunner.class)
//读取哪个配置文件中的内容
@ContextConfiguration(“classpath:applicationContext1.xml”)
public class Test1 {
//Autowired:自动注解 按类型
@Autowired
@Qualifier(“treeProxy”)//Qualifier 按id
private TreeBiz treebiz;
@Test
public void test1(){
Tree tree=new Tree();
tree.setName(“樟树”);
treebiz.save(tree);
}
}