@Service是用于服务层的IServiceImpl类文件,功能与@Repository类似。 另外标签:@Autowired 用来注入。 例如: [java] view plain copy @Autowired private UserDao userDao; 这样就注入进去了,相当于我们new了个实现类,我们就无需写setter方法了。
我们还得有配置文件进行配置:
[html] view plain copy <?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config/> <context:component-scan base-package="com.syx.manager"> <context:include-filter type="regex" expression=".*DaoImpl"/> <context:include-filter type="regex" expression=".*ServiceImpl"/> </context:component-scan> </beans>
这样就把com.syx.manager包下的所有.*DaoImpl,.*ServiceImpl都注册关联到Spring容器中去了。