Add context to our application.

main/resources/applicationContext.xml:

<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config /> <context:component-scan base-package="com.pluralsight" />
</beans>

Using annotation for @Service @Repository @Controller:

/repository/HibernateCustomerRepositoryImpl.java:

package com.pluralsight.repository;

import com.pluralsight.model.Customer;

import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List; @Repository("customerRepository")
public class HibernateCustomerRepositoryImpl implements CustomerRepository { @Override
public List<Customer> findAll() {
List<Customer> customers = new ArrayList<>();
Customer customer = new Customer(); customer.setFirstname("Byran");
customer.setLastname("John"); customers.add(customer); return customers;
}
}

/service/CustomerServiceImpl.java:

We use @Autowrie for the member of Repository.

package com.pluralsight.service;

import com.pluralsight.model.Customer;
import com.pluralsight.repository.CustomerRepository; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import java.util.List; @Service("customerService")
public class CustomerServiceImpl implements CustomerService { @Autowired
private CustomerRepository customerRepository; @Override
public List<Customer> findAll() {
return customerRepository.findAll();
} }

We can also use Setter Injection:

    private CustomerRepository customerRepository;

    @Autowired
public CustomerServiceImpl (CustomerRepository customerRepository) {
this.customerRepository = customerRepository;
}

We can also use Constructor Injection:

    private CustomerRepository customerRepository;

    @Autowired
public CustomerServiceImpl (CustomerRepository customerRepository) {
this.customerRepository = customerRepository;
}

After using Annotations & Autowired, we code looks simple and decoupled

最新文章

  1. iOS 判断View 是否是第一次显示
  2. epoch和Iteration
  3. 在没装VS2010的机器上运行VS2010开发的C++程序
  4. Gamma Gamma~!!!
  5. JDK的目录
  6. Struts2 中遇到的问题
  7. BZOJ3170: [Tjoi 2013]松鼠聚会
  8. 《UCD火花集1-2》读后感
  9. Junit3.8
  10. 简单3d RPG游戏 之 002 生命条(二)
  11. JAXB - Annotations, Annotations for the Schema: XmlSchema
  12. HR系统+人脸识别
  13. 有JSON中字段最好是【字符】而非【enum】想到
  14. ASP.NET MVC4实现URL伪静态
  15. c4b和c4f的区别
  16. MacOS High Sierra 引起 VirtualBox Vagrant 同步慢
  17. 将SQL for xml path(&#39;&#39;)中转义的字符正常显示
  18. LISTVIEW显示JPEG缩略图
  19. update pm storage
  20. (一)问候 HtmlUnit

热门文章

  1. vue2.0 静态prop和动态prop
  2. Fragment中获取Activity的Context (转)
  3. Which dispatch method would be used in Swift?
  4. ALTER LANGUAGE - 修改一个过程语言的定义
  5. call, apply, bind 区别
  6. CAD参数绘制实心圆弧填充(网页版)
  7. 转:函数调用的区别:_cdecl以及_stdcall
  8. 安装Subversion1.82(SVN)
  9. base64记载
  10. [C#] 对List进行分组排序后输出