spring中通过注解来实例化对象

来源:互联网 发布:待遇 知乎 编辑:程序博客网 时间:2024/06/07 11:04
  • 引用注解标签
xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="    http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans-4.1.xsd    http://www.springframework.org/schema/context     http://www.springframework.org/schema/context/spring-context-4.1.xsd">
  • 开启注解,自动扫面指定包下面的文件实例化
<context:component-scan base-package="Imp"></context:component-scan><!-- 这里为自动扫描Imp包下的文件实例化 -->
  • 持久层引用标签@Repository实现实例化
@Repository//持久层自动实例化public class BookinfoMapperImp implements BookinfoMapper{    @Override    public void Bookinfo() {    System.out.println("我是大赢家!");    }}
  • 业务层引用标签@Service实现实例化
  • 原来使用bean标签时的get方法用@Autowired代替
@Servicepublic class BookinfoServiceImp implements BookinfoService{    @Autowired    private BookinfoMapper bookinfoMapper;    @Override    public void checkBookinfo() {        bookinfoMapper.Bookinfo();    }
0 0
原创粉丝点击