一 .@Service(" xxxx")在类前的意义:
一个普通类的运行都是在容器中完成的。
低效率做法:是在applicationContext.xml文件中定义一个bean。一个还好,如果是多个(通常情况下一个项目中肯定会定义多个类的。
如果是像这样的话,applicationContext.xml不知道要手动多少个bean,还不累死!applicationContext.xml文件加载量也加重了。
高效做法:通常是在applicationContext.xml文件中写一行指定代码,使其自动加载指定路径下的包。然后在类前加上@Service("xxxxxx"),这样
就会自动注册到Spring 容器中,不用在applicationContext.xml文件中定义bean.
二. 做法:
① 在applicationContext.xml文件中加载如下代码:
<context:component-scan base-package="cn.demo.test.service.impl/>
三.举例说明:
@Service("testService")
public class UserDaoImpl extends HibernateDaoSupport implements UserDao{ ...... }
等价与直接在applicationContext.xml添加如下bean
<bean id="UserDao" class="cn.demo.test.dao.UserDaoImpl" scope="propotype" ...... /bean>
四.@Resource的使用
①通常是在一个项目类文件中编辑时突然想用到另外一个类(BaseDao中的TestDao),为了使其能够正常运行。故将(TestDao注入过来就行)
例如正在实现业务层接口类:
@Service("testService") public class TestServiceImpl implements TestService { public void say() { System.out.println("service saying hi"); } @Resource private TestDao testDao; public void save(Person person) { testDao.save(person) } public Person findPerson(Serializable id) { return testDao.findPerson(id); } }