Spring2.5注解笔记
深入的学习springside,并且由于项目的需要,和在项目中深切体会到大量的<bean/>标签的繁琐。故学习起spring的注解。
一句话:对开发人员来说:最常用的Spring2.5最常用的注解就4个:
@Resource @Qualifier @Service @Repository
由于项目中用到的是spring 2.5,和原来的spring2.0配置多少是有点出入的。
步骤:
1.导包。
将spring.jar 和 common-logging.jar。就两个包?对,就两个包。对于spring2.0不推荐用的标注,如@Autowired,@Component 咱就不用了,直接用推荐的。
3.建工程。为了测试@Repository 和@Service标签,我按以下结构建包。
src|---entity|---User.java
|--service|--UserServiceIf.java
|--impl--|
--UserServiceImpl.java
|--dao----|--UserDaoIf.java
|--impl--|
---UserDaoImpl.java
Class:User.java
package com.fun.entity;
import org.apache.commons.lang.builder.ToStringBuilder;
public class User {
private Long id;
private String name;
private String password;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public User(){}
public String toString(){
return ToStringBuilder.reflectionToString(this);//需要加common-lang.jar
}
Class:UserServiceIf.java
package com.fun.service;
public interface UserServiceIf<User,Serializable> {
public void register(User user);
}
Class:UserServiceImpl.java
package com.fun.service.impl;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import com.fun.dao.UserDao;
import com.fun.entity.User;
import com.fun.service.UserService;
@Service("userService")
public class UserServiceImpl implements UserService<User,Long> {
private UserDao<User,Long> userDao;
public void register(User user) {
userDao.save(user);
}
public UserDao<User,Long> getUserDao() {
return userDao;
}
@Resource
public void setUserDao(UserDao<User,Long> userDao) {
this.userDao = userDao;
}
}
Class:UserDaoIf.java
package com.fun.dao;
public interface UserDaoIf<User,Serializable > {
public void save(User user);
}
Class:UserDaoImpl.java
package com.fun.dao.impl;
import java.io.Serializable;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;
import com.fun.dao.UserDao;
import com.fun.entity.User;
@Repository("userDao")
public class UserDaoImpl implements UserDao<User,Serializable> {
public void save(User user) {
Assert.notNull(user);
System.out.println(user);
if(null==user.getId()){
System.out.println("save User");
}else{
System.out.println("update User");
}
}
}
applicationContext.xml
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
default-lazy-init="true">
<!-- 支持标注 -->
<context:annotation-config/>
<!-- 扫描用标注的包 -->
<context:component-scan base-package="com.fun"/>
</beans>
注:这里有个小插曲,之前一直运行没有通过,老是报org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 8 in XML document from URL [file:/E:/workspace/SpringTest-java/bin/applicationContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'context:annotation-config'.
后来才发现,原因是:在MyEclipse生成的XML头中,只加了context的命名空间,忘了加Context的Location Hint 如果没有加的话,则在 xsi:schemaLocation中没有http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
这两句,从而报错。
Class:Test.java
package com.fun.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.fun.eg.SimpleMan;
import com.fun.entity.User;
import com.fun.service.UserService;
public class Test {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
User user=new User();
user.setName("springTest");
user.setPassword("123");
ApplicationContext ac=new ClassPathXmlApplicationContext("classpath*:applicationContext.xml");
user.setId(1234L);
UserService<User, Long> us=(UserService<User,Long>)ac.getBean("userService");
us.register(user);
}
}
四,上述是可运行代码。可以先小试牛刀一下。
五.先看看不用注解的写法。
<bean id="userDao" class="com.fun.dao.impl.UserDaoImpl"/>
<bean id="userService" class="com.fun.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"/>
</bean>
其中:在UserDaoImpl中的@Repository(“userDao”) 等于
<bean id="userDao" class="com.fun.dao.impl.UserDaoImpl"/>
在UserServiceImpl中的@Service(“userService”)等于
<bean id="userService" class="com.fun.service.impl.UserServiceImpl"/>
在 UserServiceImpl中的@Resource等于
<property name="userDao" ref="userDao"/>
在Spring的注解中 采用了 Convertion over Configuration(约定优于配置)的方式。但是,如果在UserDaoImpl中写成@Repository(”myUserDao”)
那么按原来的写法就会报BeanCreationException,因为我们在UserServiceImpl中写的是userDao,这时,@Qualifier就派上用场了,UserSImpl的setUserDao方法中应改写成
@Resource
public void setUserDao(@Qualifier(“myUserDao”)UserDao<User,Long> userDao) {
this.userDao = userDao;
}
参考:
http://macrochen.javaeye.com/blog/323667
http://www.javaeye.com/topic/295348
发表于 @ 2009年10月13日 09:41:00 | 评论( 0 ) | 编辑| 举报| 收藏
旧一篇:马云语录 经典
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/Beacher_Ma/archive/2009/10/13/4662421.aspx
posted on 2009-10-13 11:11 流浪汉 阅读(634) 评论(0) 编辑 收藏 引用 所属分类: java 、web