[Java Sprint] Spring Configuration Using Java
There is no applicationContext.xml file.
- Too much XML
- Namespaces helped
- Enter Java Configuration
Create main/java/com.pluralsight/AppConfig.java:
1. Setter Injection:
package com.pluralsight; import com.pluralsight.repository.CustomerRepository;
import com.pluralsight.repository.HibernateCustomerRepositoryImpl;
import com.pluralsight.service.CustomerService;
import com.pluralsight.service.CustomerServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class AppConfig { @Bean(name = "customerService")
public CustomerService getCustomerService() {
CustomerServiceImpl service = new CustomerServiceImpl(); // Setter Injection
service.setCustomerRepository(getCustomerRepository());
return service;
} @Bean(name = "customerRepository")
public CustomerRepository getCustomerRepository () {
return new HibernateCustomerRepositoryImpl();
}
}
Setter Injection for Service:
package com.pluralsight.service; import com.pluralsight.model.Customer;
import com.pluralsight.repository.CustomerRepository; import org.springframework.stereotype.Service; import java.util.List; @Service("customerService")
public class CustomerServiceImpl implements CustomerService { private CustomerRepository customerRepository; // Setter Injection
public void setCustomerRepository(CustomerRepository customerRepository) {
this.customerRepository = customerRepository;
} @Override
public List<Customer> findAll() {
return customerRepository.findAll();
} }
2. Constructor Injection:
package com.pluralsight; import com.pluralsight.repository.CustomerRepository;
import com.pluralsight.repository.HibernateCustomerRepositoryImpl;
import com.pluralsight.service.CustomerService;
import com.pluralsight.service.CustomerServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class AppConfig { @Bean(name = "customerService")
public CustomerService getCustomerService() {
CustomerServiceImpl service = new CustomerServiceImpl(getCustomerRepository());
return service;
} @Bean(name = "customerRepository")
public CustomerRepository getCustomerRepository () {
return new HibernateCustomerRepositoryImpl();
}
}
package com.pluralsight.service; import com.pluralsight.model.Customer;
import com.pluralsight.repository.CustomerRepository; import org.springframework.stereotype.Service; import java.util.List; @Service("customerService")
public class CustomerServiceImpl implements CustomerService { private CustomerRepository customerRepository; // Constructor Injection
public CustomerServiceImpl(CustomerRepository customerRepository) {
this.customerRepository = customerRepository;
} @Override
public List<Customer> findAll() {
return customerRepository.findAll();
} }
3. Autowired:
It would be good to add @Service and @Repository to each java files:
@Service("customerService")
public class CustomerServiceImpl implements CustomerService {
@Repository("customerRepository")
public class HibernateCustomerRepositoryImpl implements CustomerRepository {
Add @ComponentScan({"com.pluralsight"}) to the AppConfig.java:
package com.pluralsight; import com.pluralsight.repository.CustomerRepository;
import com.pluralsight.repository.HibernateCustomerRepositoryImpl;
import com.pluralsight.service.CustomerService;
import com.pluralsight.service.CustomerServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; @Configuration
@ComponentScan({"com.pluralsight"})
public class AppConfig {
/*
@Bean(name = "customerService")
public CustomerService getCustomerService() {
CustomerServiceImpl service = new
return service;
} @Bean(name = "customerRepository")
public CustomerRepository getCustomerRepository () {
return new HibernateCustomerRepositoryImpl();
}*/
}
The prower of Autowired is that, we don't need to define any @Bean in AppConfig.
[Java Sprint] Spring Configuration Using Java的更多相关文章
- [Java Sprint] Spring XML Configuration : Constructor Injection Demo
Previous we see how to do Setter injection: https://www.cnblogs.com/Answer1215/p/9472117.html Now le ...
- [Java Sprint] Spring XML Configuration : Setter Injection Demo
In CustomerServiceImpl.java, we hardcoded 'HibernateCustomerRepositoryImpl' package com.pluralsight. ...
- java 之 Spring 框架(Java之负基础实战)
1.Spring是什么 相当于安卓的MVC框架,是一个开源框架.一般用于轻型或中型应用. 它的核心是控制反转(IoC)和面向切面(AOP). 主要优势是分层架构,允许选择使用哪一个组件.使用基本的Ja ...
- JAVA模拟Spring实现IoC过程(附源码)
前言:本人大四学生,第一次写博客,如果有写得不好的地方,请大家多多指正 一.IoC(Inversion of Control)反转控制 传统开发都是需要对象就new,但这样做有几个问题: 效率低下,创 ...
- Redis(Windows安装方法与Java调用实例 & 配置文件参数说明 & Java使用Redis所用Jar包 & Redis与Memcached区别 & redis-cli.exe命令及示例)
Windows下Redis的安装使用 0.前言 因为是初次使用,所以是在windows下进行安装和使用,参考了几篇博客,下面整理一下 1.安装Redis 官方网站:http://redis.io/ 官 ...
- Spring的Java配置方式—@Configuration和@Bean实现Java配置
Java配置是Spring4.x推荐的配置方式,可以完全替代xml配置. 1.@Configuration 和 @BeanSpring的Java配置方式是通过 @Configuration 和 @Be ...
- Mybatis Spring multiple databases Java configuration
https://stackoverflow.com/questions/18201075/mybatis-spring-multiple-databases-java-configuration ** ...
- 利用spring boot创建java app
利用spring boot创建java app 背景 在使用spring框架开发的过程中,随着功能以及业务逻辑的日益复杂,应用伴随着大量的XML配置和复杂的bean依赖关系,特别是在使用mvc的时候各 ...
- spring+mybati java config配置引起的bean相互引用日志报警告问题
摘要: Error creating bean with name 'XXX': Requested bean is currently in creation: Is there an unreso ...
随机推荐
- WebSocket 的一些简单页面推送使用
因为做通信项目的时候,需要实时获取每个分机的当前状态,发现websocket还不错,只是对浏览器的要求比较高, 针对特定用户推送消息,网上有一些 public class GetHttpSession ...
- 迅为4418开发板Qt移植移动4G模块第二部分
第一部分: http://www.cnblogs.com/topeet/p/6509248.html 第二部分: 5.ping不通域名一般是DNS没有设置对造成的.在etc下有一个文件resolv.c ...
- ubuntu 网卡配置
- JVM优化(中)
09.垃圾收集器之串行垃圾收集器: 1.-Xms512m 等价于 -XX:InitialHeapSize=512设置JVM初始堆内存大小:-Xmx2048m 等价于 -XX:MaxHeapSize=2 ...
- 奇异值分解 SVD 的数学解释
奇异值分解(Singular Value Decomposition,SVD)是一种矩阵分解(Matrix Decomposition)的方法.除此之外,矩阵分解还有很多方法,例如特征分解(Eigen ...
- LOJ 2321 清华集训2017 无限之环 拆点+最小费用最大流
题面:中文题面,这里不占用篇幅 分析: 看到题面,我就想弃疗…… 但是作为任务题单,还是抄了题解…… 大概就是将每个格子拆点,拆成五个点,上下左右的触点和一个负责连源汇点的点(以下简称本点). 这个这 ...
- C++中const与constexpr区别
对于对象来说 const指的是编译期常量和运行时常量,两者并没有区分 constexpr特指编译期常量 对于函数来说 const可以修饰类的成员函数,被修饰的函数在执行期间不会改变对象的值. clas ...
- Windows下Eclipse+PyDev安装Python开发环境
.简介 Eclipse是一款基于Java的可扩展开发平台.其官方下载中包括J2EE方向版本.Java方向版本.C/C++方向版本.移动应用方向版本等诸多版本.除此之外,Eclipse还可以通过安装插件 ...
- Python之购物车
Python之购物车 msg_list = [ ['iphone',8888], ['coffe',38], ['book',90], ['Tesla',100000], ['RR',10000000 ...
- Python基础 - pip导出依赖环境和安装依赖环境的命令
导出: pip freeze > requirements.txt 安装: pip install -r requirements.txt