[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 ...
随机推荐
- 使用迅为iTOP-iMX6开发板-uboot-修改默认环境变量
iTOP-iMX6 开发板烧写好之后,默认是 android 系统 9.7 寸屏幕的系统参数和屏幕参数.如下图.本文档主要介绍如何修改默认启动参数. 1. 重要的环境变量比较重要的环境变量或者说经常使 ...
- app dcloud 打包公用证书
Android平台云端打包使用的DCloud公用证书 分类:HTML5+ 5+App开发 HBuilder|HBuilderX应用云端打包Android平台默认使用的DCloud公用证书,其信息如下: ...
- SVG 浏览器支持
可以参考以下链接: https://caniuse.com/#search=svg https://en.wikipedia.org/wiki/Comparison_of_layout_engines ...
- CAD交互绘制圆(com接口)
CAD绘制图像的过程中,画圆的情况是非常常见的,用户可以在控件视区点取任意一点做为圆心,再动态点取半径绘制圆. 主要用到函数说明: _DMxDrawX::DrawCircle 绘制一个圆.详细说明如下 ...
- 文件描述符 文件操作 <> open 文件句柄
#! /usr/bin/perl use strict;use warnings; =head1print "\n---------------------------------test_ ...
- 第1节 yarn:13、yarn资源调度的介绍
Yarn资源调度 yarn集群的监控管理界面: http://192.168.52.100:8088/cluster jobHistoryServer查看界面: http://192.168.52.1 ...
- thinkphp5入口文件对应模块绑定
在配置文件config.php中找到或者添加 // 入口自动绑定模块 'auto_bind_module' => true, 修改其属性为true
- 每日命令:(12)sar
sar(System Activity Reporter系统活动情况报告)是目前 Linux 上最为全面的系统性能分析工具之一,可以从多方面对系统的活动进行报告, 包括:文件的读写情况.系统调用的使用 ...
- 修改Python的镜像源
Mac OS下修改Python的镜像源 步骤: 切换到家目录 创建目录 .pip 并切换到该目录 创建 pip.conf 文件并写入配置信息 [global] index-url = https:// ...
- 读书笔记:《人有人的用处》------N.维纳. (2016.12.28)
读书笔记:<人有人的用处>------N.维纳 ·某些系统可以依其总能量而和其他系统区别开来. ·在某些情况下,一个系统如果保持足够长时间的运转,那它就会遍历一切与其能量相容的位置和动量的 ...