注解方式基本会了,现在想学用配置方式注入hibernate和struts,想请教一下各位csdn大神。
比如,现在我们有一个UserBean,它有了User.hbm.xml,有UserService这个接口,有UserServiceImpl这个实现类,有UserDao这个持久层接口,有UserDaoImpl实现类。那么,原来的注解方式spring配置文件关于此部分的配置是:
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!--使用基于注解方式配置事务 -->
<tx:annotation-driven transaction-manager="txManager"/>现在这里应该怎么写呢??
<!-- 1.将sessionFactory注入到持久层 -->
<bean id="lucenDaoImpl" class="cn.dao.impl.LucenDaoImpl" scope="prototype">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 事务管理 --> <!-- 2.将持久层注入到业务层 -->
<bean id="lucenServiceImpl" class="cn.service.impl.LucenServiceImpl" scope="prototype">
<property name="这里到底是注入的是dao,还是daoImpl??" ref="这里又该怎么写??"></property>
</bean>
<!-- 3.将业务层注入到表现层 -->还有,这种写法的话,spring的文件会很大,所以会分开写,但是分开后,程序怎么知道相应的spring文件是那些?在哪儿?
是不是要在spring的主文件里告诉它呢???
种种问题,还请高手们一一指点,小弟感激不尽啊。
比如,现在我们有一个UserBean,它有了User.hbm.xml,有UserService这个接口,有UserServiceImpl这个实现类,有UserDao这个持久层接口,有UserDaoImpl实现类。那么,原来的注解方式spring配置文件关于此部分的配置是:
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!--使用基于注解方式配置事务 -->
<tx:annotation-driven transaction-manager="txManager"/>现在这里应该怎么写呢??
<!-- 1.将sessionFactory注入到持久层 -->
<bean id="lucenDaoImpl" class="cn.dao.impl.LucenDaoImpl" scope="prototype">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 事务管理 --> <!-- 2.将持久层注入到业务层 -->
<bean id="lucenServiceImpl" class="cn.service.impl.LucenServiceImpl" scope="prototype">
<property name="这里到底是注入的是dao,还是daoImpl??" ref="这里又该怎么写??"></property>
</bean>
<!-- 3.将业务层注入到表现层 -->还有,这种写法的话,spring的文件会很大,所以会分开写,但是分开后,程序怎么知道相应的spring文件是那些?在哪儿?
是不是要在spring的主文件里告诉它呢???
种种问题,还请高手们一一指点,小弟感激不尽啊。
<bean id="UserServiceImpl" class="*.*.UserServiceImpl">
<property name="UserDao" ref="UserDaoImpl"/>
</bean>在UserServiceImpl里的成员变量是接口,让Spring给你注入实现类,像下面这样public class UserServiceImpl{
private UserDao userDao;
//getter...
//setter...
}1.在web.xml文件中配置多个Spring配置文件<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext-dao.xml,
classpath:applicationContext2-service.xml,
<!-- add more 最后一个结尾不带逗号-->
</param-value>
</context-param>在applicationContext.xml主文件内引用多个文件<import resource="applicationContext-dao.xml"/>
<import resource="applicationContext-service.xml"/>
<!-- add more -->
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext-dao.xml,
classpath:applicationContext2-service.xml,
<!-- add more 最后一个结尾不带逗号-->
</param-value>
</context-param>
这个我倒是没用过。是直接用
ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml","applicationContext_DAO.xml"});
UserDAOImpl userDAO = (UserDAOImpl)ctx.getBean("userDAO");
<property name="这里写你LucenServiceImpl类里面的属性名" ref="这里写需要实例化的类"></property>
</bean>
比如:
public class LucenServiceImpl { private UserDAOImpl udi;如果有父类就用父类
必须给set方法,才能让spring注入成功
public void setUdi(UserDAOImpl udi){
this.udi=udi
}}
name里的udi对应类里面的UserDAOImpl udi对象 ref里写另一个bean的id 这个bean实例化的对象必须是前面name对应的属性的子类或是自己才能注入成功
<bean id="UserDAOImpl" class="cn.service.impl.UserDAOImpl" scope="prototype">
</bean>
<bean id="lucenServiceImpl" class="cn.service.impl.LucenServiceImpl" scope="prototype">
<property name="udi" ref="UserDAOImpl"></property>
</bean>
<import resource="applicationContext-service.xml"/>
用import导入就可以了
我完全按照你说的来写的。但是始终报错,代码如下: <!-- 1.将sessionFactory注入到持久层 -->
<bean id="LucenDaoImpl" class="cn.microbar.dao.impl.LucenDaoImpl" scope="prototype">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 事务管理 --> <!-- 2.将持久层注入到业务层 -->
<bean id="LucenServiceImpl" class="cn.microbar.service.impl.LucenServiceImpl" scope="prototype">
<property name="LucenDao" ref="LucenDaoImpl"></property>
</bean>这里我不能理解的是,sessionfactory的事物是怎么定义开启的呢??只要将它注入到dao的bean里就行了吗?
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'lucenServiceImpl' is defined
。
。。
public class LucenTest { private static LucenService lucenService;
@BeforeClass
public static void setUpBeforClass() throws Exception{
try{
ApplicationContext act = new ClassPathXmlApplicationContext("springBean.xml");
lucenService = (LucenService)act.getBean("lucenServiceImpl");
}catch (RuntimeException e){
e.printStackTrace();
}
}
@Test
public void save(){
lucenService.save(new Lucen("d123456","23423588"));
}
}
。加上上面的写法。报错就是上面的错
哥哥。我发现你的整体结构比较清晰嘞能把你的整个spring配置文件怎么分层的,将给我听,,好么?也就是。。你的那些主applicationContext.xml里怎么写。。然后分类的applicationContext.xml怎么写的???
实在您要是不介意呢把您的框架源码给我个大概咯。呵呵。。有点简单的程序就行。。呵呵主要是用来学习小弟没人教导啊没有进过专业培训机构