[Java Spring] Spring Annotation Configuration Using XML
2024-08-30 18:31:26
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
最新文章
- iOS 判断View 是否是第一次显示
- epoch和Iteration
- 在没装VS2010的机器上运行VS2010开发的C++程序
- Gamma Gamma~!!!
- JDK的目录
- Struts2 中遇到的问题
- BZOJ3170: [Tjoi 2013]松鼠聚会
- 《UCD火花集1-2》读后感
- Junit3.8
- 简单3d RPG游戏 之 002 生命条(二)
- JAXB - Annotations, Annotations for the Schema: XmlSchema
- HR系统+人脸识别
- 有JSON中字段最好是【字符】而非【enum】想到
- ASP.NET MVC4实现URL伪静态
- c4b和c4f的区别
- MacOS High Sierra 引起 VirtualBox Vagrant 同步慢
- 将SQL for xml path(&#39;&#39;)中转义的字符正常显示
- LISTVIEW显示JPEG缩略图
- update pm storage
- (一)问候 HtmlUnit
热门文章
- vue2.0 静态prop和动态prop
- Fragment中获取Activity的Context (转)
- Which dispatch method would be used in Swift?
- ALTER LANGUAGE - 修改一个过程语言的定义
- call, apply, bind 区别
- CAD参数绘制实心圆弧填充(网页版)
- 转:函数调用的区别:_cdecl以及_stdcall
- 安装Subversion1.82(SVN)
- base64记载
- [C#] 对List进行分组排序后输出