在spring中提供了如下几个Annotation来标注Spring Bean:
- @Component:标注一个普通的Spring Bean类。
- @Controller:标注一个控制器组件类。
- @Service:标注一个业务逻辑组件类。
- @Repository:标注一个Dao组件类。
另外两个常用的Spring Annotation:
- @Autowired:用来自动装配Bean,可以标注setter方法、普通方法、Field和构造器等。
- @Scope:用来标注一个Spring Bean的作用域,默认是singleton,对于action类我们会通常将其scope设置为prototype。
下面看如何使用Spring的annotation,首先需要在Spring的配置文件applicationContext.xml中添加如下配置:
<context:component-scan base-package="zwh.ssh.maven"></context:component-scan>
这里package属性要设置成你的组件所在的包,添加该语句后Spring会自动搜索该包和子包下的Bean组件。另外为了applicationContext.xml识别context的标签前缀,需要添加context的命名空间和其使用shema文件位置,其内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- derby创建用户名和密码参考:http://www.joyzhong.com/archives/643 -->
<bean
id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>org.apache.derby.jdbc.EmbeddedDriver</value>
</property>
<property name="url">
<value>jdbc:derby:f:/zwh/mydb2</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>root</value>
</property>
</bean>
<bean
id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect"> org.hibernate.dialect.DerbyDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>zwh.ssh.maven.po.User</value>
</list>
</property>
</bean>
<context:component-scan base-package="zwh.ssh.maven"></context:component-scan>
</beans>
接下来给我的代码添加annotation,首先为dao添加:
@Repository("userDao")
public class UserDaoDerbyImpl implements UserDao {
@Autowired
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public List<User> findAllUser(){
Session session = this.sessionFactory.openSession();
List<User> list = session.createCriteria(User.class).list();
session.close();
return list;
}
}
这里的sessionFactory还是在xml文件里配置的,使用@Autowired来自动装配xml文件里的sessionFactory。并将该Dao类使用@Repository("userDao")标注,其id属性是userDao,自动装配时会根据该属性值将该对象注入进去。
为service添加annotation:
@Service("userService")
public class UserServiceImpl implements UserService {
Logger log = Logger.getLogger(UserService.class);
@Autowired
private UserDao userDao;
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public boolean login(String username,String password){
log.info("正在执行login方法....");
List<User> users =this.userDao.findAllUser();
for(User user : users){
if(user.getUsername().equals(username)){
if(user.getPassword().equals(password)){
return true;
} else {
return false;
}
}
}
return false;
}
public boolean check(String username) {
List<User> users =this.userDao.findAllUser();
for(User user : users){
if(user.getUsername().equals(username)){
return true;
}
}
return false;
}
}
注意这里@Autowired标注的Field的是userDao,与前面@Repository里面的内容是一致的。
action的annotation:
@Scope("prototype")
@Controller
public class LoginAction implements Action {
@Autowired
private UserService userService;
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
public String execute(){
String pwdMD5 = DigestUtils.md5Hex(user.getPassword());
if(this.userService.login(user.getUsername(), pwdMD5)){
ActionContext.getContext().getSession().put("username", this.user.getUsername());
return SUCCESS;
}
return ERROR;
}
}
这里使用@Scope("prototype")来指定了该action是原型类型的,即每来一个请求都要new一个这个类的对象,而不是使用单例,使用单例时如果有多个请求同时请求该action就会出现错误。
分享到:
相关推荐
struts2+spring2.5+hibernate3.2 annotation配置完整eclipse项目,带数据库脚本
个人认为由Sun官方支持的EJB规范会越来越流行,此时如果使用基于Annotation的SSH框架很容易转移到Struts+EJB+Spring的项目中,而且使用Annotation,很容易实现0配置,像在这个实例中就一个配置,这样避免了配置文件...
实现了简单用户权限登录,项目中含有mysql数据库 加入了基本的拦截器,错误类处理等 加入了BaseDao,Spring3对Hibernate4已经没有了HibernateDaoSupport和HibernateTemplate的支持,使用了原生态的API
一、 开始使用annotation配置Spring 16 二、 @Autowired、@Qualifier 16 (一) @Autowired 16 (二) @Qualifier 17 三、 @Resource(重要、推荐) 17 (一) JSR-250 17 (二) @Resource 17 四、 @Componet 18 五、 @Scope...
Spring个人总结,基于Annotation注解的方式开发,配置
项目访问URL时:http://localhost:8080/test_ssh/userLogin struts2+spring+hibernate(实现XML和Annotation两种方式操作数据库) 项目描述: 框架及版本:struts2 + spring3.0 + ...注意spring配置文件的配置
项目使用的技术 支持 在这里连接到oracle db,但是如果使用mysql,同样的方法,只需查看hikaricp的配置就可以了 项目安装指南 版本 版本1.0 [最新更新]特别注意 每次都要运行grunt watch的reactjs代码 建议并行...
一、 开始使用annotation配置Spring 16 二、 @Autowired、@Qualifier 16 (一) @Autowired 16 (二) @Qualifier 17 三、 @Resource(重要、推荐) 17 (一) JSR-250 17 (二) @Resource 17 四、 @Componet 18 五、 @Scope...
这个最早源于spring2.x时代的spring-modules项目中的cache子项目 我自己也曾经仿造者,并且基于aspectj山寨过过aop annotation cache 在大部分简单的cache场景都是非常好用的 少部分需要精确evict key的场景还不适合...
内置一个基于数据库的代码生成器rapid-generator,极易进行二次开发 struts1,struts2的零配置 spring集成及加强,自动搜索hibernate的entity annotation class 集成动态构造sql的工具:rapid-xsqlbuilder 集成...
4.11.2 使用基于Java类的配置信息启动Spring容器 4.12 不同配置方式比较 4.13 小结 第5章 Spring容器高级主题 5.1 Spring容器技术内幕 5.1.1 内部工作机制 5.1.2 BeanDefinition 5.1.3 InstantiationStrategy 5.1.4 ...
spring boot —— redis 缓存注解使用教程 示例项目地址: 依赖 在pom文件添加如下依赖 <groupId>org.springframework.boot <artifactId>spring-boot-starter-data-redis 配置 在application.yml配置文件添加...
6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点运算 ...
Spring 生态中的位置:该项目主要的目的是:为 Spring 的开发提供了更快更广泛的快速上手使用默认方式实现快速开发提供大多数项目所需的非功能特性,诸如:嵌入式服务器、安全、心跳检查、外部配置等Spring Boot 不...
6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点运算 ...
该项目使用Gradle进行构建项目,并且简单使用了testNG做单元测试 项目说明 在配置freemarker作为模版时,使用xml作为mvc配置文件,目前用testng进行集成测试无法通过 下载项目之后,运行gradle idea生成idea项目 ...
在搭建spring项目时通常需要这些jar包 commons-logging-1.1.3.jar spring-aop-4.0.6.RELEASE.jar spring-beans-4.0.6.RELEASE.jar spring-context-4.0.6.RELEASE.jar spring-core-4.0.6.RELEASE.jar spring-...
创建一个继承自org.springframework.cache.annotation.CachingConfigurerSupport的配置类,并重写其中的cacheManager()方法。 3.使用缓存注解: 在需要进行缓存的方法上添加缓存注解。例如,使用@Cacheable注解来...
此附件是:Spring3.1+Hibernate4.0+Struts2.3.1 使用annotation的零配置项目.部分功能已实现.虽然已经实现了功能.但有些原理,还待大家一起研讨.
4.11.2 使用基于Java类的配置信息启动Spring容器 4.12 不同配置方式比较 4.13 小结 第5章 Spring容器高级主题 5.1 Spring容器技术内幕 5.1.1 内部工作机制 5.1.2 BeanDefinition 5.1.3 InstantiationStrategy 5.1.4 ...