Spring第六讲学习笔记,使用注解开发。Repository,Service,Controller
来源:互联网 发布:sp最短路径优先算法 编辑:程序博客网 时间:2024/06/18 17:24
把spring中user模块的user.xml配置文件中,之前为UserAction配置userService的时候,struts的配置文件
Action.UserAction。这样为注解开发提供了条件。
使用注解开发,
1.修改Spring头文件,增加context。
2.在Spring配置文件中写
<context:component-scan base-package="dao.imp"/>
<context:component-scan base-package="service.imp"/>
<context:component-scan base-package="action"/>
这个时候,突然发现如果包名前缀是相同的,如cn.sxt....,那么这里就能直接写
<context:component-sacn base-package:"cn.sxt"/>
3.在UserDaoImpl中自动注入sqlSessionFactory
在UserDao的实现类的上一行写
@Repository("userDao") :数据仓库,相当于<bean id="userDao" class="实现类">,
@Repository("userDao")
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao{
@Autowired
@Override
public voidsetSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
// TODO Auto-generated method stub
super.setSqlSessionFactory(sqlSessionFactory);
}
4.在UserServiceImpl中自动注入userDao
@Service("userService")
public class UserServiceImpl implements UserService{
@Autowired
private UserDao userDao;
5.在Action中自动注入userService
@Autowired
private UserService userService;
中的class可以填这个bean的id,现在可以把这种方法干掉。仍然使用以前struts的方法,
class="action.UserAction",而不是bean的ID。这还是让struts来管理action。
在struts的配置文件中,让struts创建userService对象,struts的配置文件中class写Action.UserAction。这样为注解开发提供了条件。
使用注解开发,
1.修改Spring头文件,增加context。
省去Spring模块的user.xml中UserDao中sqlSessionFactory,
UserService中的userDao,
UserAction中的userService的bean注入
2.在Spring配置文件中写
<context:component-scan base-package="dao.imp"/>
<context:component-scan base-package="service.imp"/>
<context:component-scan base-package="action"/>
这个时候,突然发现如果包名前缀是相同的,如cn.sxt....,那么这里就能直接写
<context:component-sacn base-package:"cn.sxt"/>
3.在UserDaoImpl中自动注入sqlSessionFactory
在UserDao的实现类的上一行写
@Repository("userDao") :数据仓库,相当于<bean id="userDao" class="实现类">,
这里是在实现类里,@Repository括号里的相当于id,
可以通过beanFactory.getBean("userDao")拿到UserDao接口的实现类。
接下来自动注入sqlSessionFactory字段,调用父类的setSqlSessionFactory方法,
在set方法上写@Autowired以便自动注入,要么在字段上写。
@Repository("userDao")
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao{
@Autowired
@Override
public voidsetSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
// TODO Auto-generated method stub
super.setSqlSessionFactory(sqlSessionFactory);
}
4.在UserServiceImpl中自动注入userDao
在类的上一行写@Service("userService"),如果填写括号里的userService,那么默认是类名,
就是@Service("UserServiceImpl")
在userDao上写@Autowired,当然因为是自动注入,所以必须要有get和set方法@Service("userService")
public class UserServiceImpl implements UserService{
@Autowired
private UserDao userDao;
5.在Action中自动注入userService
在类的上一行写@Controller("userAction"),说白了,括号就是给类添加一个标签,
在测试类中的beanFactory.getBean("userAction")能够获取到对应类的实例对象。
在userService上写@Autowired,为userService自动注入,同理必须有set和get方法
@Controller("userAction")
@Scope("prototype")
public class UserAction implements Action{@Autowired
private UserService userService;
阅读全文
0 0
- Spring第六讲学习笔记,使用注解开发。Repository,Service,Controller
- Spring注解@Component、@Repository、@Service、@Controller使用
- Spring注解@Repository、@Service、@Controller
- spring注解:@Repository、@Service、@Controller
- Spring学习笔记(一):Spring注解@Component、@Repository、@Service、@Controller区别
- Spring注解开发 作用区别@Component、@Repository、@Service、@Controller
- Spring学习1_Spring注解@Component、@Repository、@Service、@Controller区别
- Spring注解的使用和区别:@Component、@Service、@Repository、@Controller
- Spring注解的使用和区别:@Component、@Service、@Repository、@Controller
- Spring注解使用以及@Component、@Repository、@Service、@Controller区别
- spring注解-@Component(不推荐使用)、@Repository、@Service、@Controller
- 使用spring注解@Controller @Service @Repository简化配置
- 使用spring注解@Controller @Service @Repository简化配置
- 使用spring注解@Controller @Service @Repository简化配置
- 使用spring注解@Controller @Service @Repository简化配置
- Spring注解@Component、@Repository、@Service、@Controller区别
- Spring注解@Component、@Repository、@Service、@Controller区别
- Spring 注解@Component,@Service,@Controller,@Repository
- Canvas之画柱状图
- 我的java学习之路2
- URL编码以及GET和POST提交乱码解决方案
- XmlSerializer 生成Xml文件
- 归并排序—Java版
- Spring第六讲学习笔记,使用注解开发。Repository,Service,Controller
- Python 实现列表 偶数位与奇数反转
- pod setup 提示错误解决方法
- java微信公众号开发环境准备
- hdoj1088 Write a simple HTML Browser(在于细心)
- Makefile VS shell
- Bootstrap响应式导航由768px变成992px
- 如何训练你的神经网络?
- android 好的代码习惯