Web 应用程序采用了经典的三层分层结构的话,最好在持久层、业务层和控制层分别采用 @Repository、@Service 和 @Controller 对分层中的类进行注释;
实际使用中
实例一:@Repository
上图中的application3.xml是配置文件,test2是测试类。其他的不用管;
配置文件是启动spring容器时扫描com.introduce.Dao所有的注解。
test2测试类
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.introduce.Dao.UserDao;
public class Test2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext3.xml");
UserDao userDao=ac.getBean(UserDao.class);
userDao.getUser2("haha");
}
}
package com.introduce.Dao;
import com.introduce.Model.User2;
public interface UserDao{
User2 getUser2();
User2 getUser2(String name);
}
package com.introduce.Dao;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import com.introduce.Model.User2;
@Service
public class UserDaoImpl implements UserDao{
// private SqlSessionFactory sqlSessionFactory;
// public UserDaoImpl(SqlSessionFactory sqlSessionFactory){
// this.sqlSessionFactory=sqlSessionFactory;
// }
@Override
public User2 getUser2() {
return null;
}
@Override
public User2 getUser2(String name) {
// TODO Auto-generated method stub
// SqlSession sqlSession=sqlSessionFactory.openSession();
// User2 user2=sqlSession.selectOne("com.introduce.Modle.User2.getUser2",name);
User2 user2=new User2();
user2.setAge(22);
System.out.println(name);
return user2;
}
}
注:至于注解中@Service()的括号中要不要加字符串,如@Service("userDao").这里是加不加,加什么样的字符都无所谓,因为只有一个实现类,当有多个实现类是,也可以不用填,在下面的实例二中,会通过@Qualifier()来指定特定的实现了。如果填了,要保证两个字符串不相等即可。一般项目中安全起见会以实现类名为准,避免重复。
实例二:@Repository 与service一起使用,并当有多个实现类时,如何指定某个实现类。
测试类 main方法:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.introduce.Dao.UserDao;
import com.introduce.Dao.UserServiceImpl;
import com.introduce.Dao.UserService;
public class Test2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext3.xml");
UserService userService=ac.getBean(UserService.class);
userService.ff();
// UserDao userDao=ac.getBean(UserDao.class);
// userDao.getUser2("haha");
}
}
进入到service接口和实现类
package com.introduce.Dao;
public interface UserService {
void ff();
}
package com.introduce.Dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import com.introduce.Dao.UserDao;
@Service
public class UserServiceImpl implements UserService1{
@Autowired
@Qualifier("userDaoImpl2")
UserDao userDao;
public void ff(){
userDao.getUser2("oo");
}
}
在service的实现类中有两点,一是加@Service;二是使用@Qualifier()指定某个实现类。@Qualifier()括号中的字符串某人是实现类的类名首字母小写,当然也可以自己定义,如在实现类的其中一个service("s1"),另一个service("s2");即可。下面的代码是没加的。可以手动加,然后再@Qualifier('s2')就会执行userDaoImpl类的方法。
dao层的三个类,一个接口,两个实现类
package com.introduce.Dao;
import com.introduce.Model.User2;
public interface UserDao{
User2 getUser2();
User2 getUser2(String name);
}
package com.introduce.Dao;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import com.introduce.Model.User2;
@Service
public class UserDaoImpl implements UserDao{
// private SqlSessionFactory sqlSessionFactory;
// public UserDaoImpl(SqlSessionFactory sqlSessionFactory){
// this.sqlSessionFactory=sqlSessionFactory;
// }
@Override
public User2 getUser2() {
return null;
}
@Override
public User2 getUser2(String name) {
// TODO Auto-generated method stub
// SqlSession sqlSession=sqlSessionFactory.openSession();
// User2 user2=sqlSession.selectOne("com.introduce.Modle.User2.getUser2",name);
User2 user2=new User2();
user2.setAge(22);
System.out.println(name);
return user2;
}
}
package com.introduce.Dao;
import org.springframework.stereotype.Service;
import com.introduce.Model.User2;
@Service
public class UserDaoImpl2 implements UserDao{
@Override
public User2 getUser2() {
return null;
}
@Override
public User2 getUser2(String name) {
// TODO Auto-generated method stub
// SqlSession sqlSession=sqlSessionFactory.openSession();
// User2 user2=sqlSession.selectOne("com.introduce.Modle.User2.getUser2",name);
User2 user2=new User2();
user2.setAge(22);
System.out.println(name);
return user2;
}
}