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的更多相关文章

  1. [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 ...

  2. [Java Sprint] Spring XML Configuration : Setter Injection Demo

    In CustomerServiceImpl.java, we hardcoded 'HibernateCustomerRepositoryImpl' package com.pluralsight. ...

  3. java 之 Spring 框架(Java之负基础实战)

    1.Spring是什么 相当于安卓的MVC框架,是一个开源框架.一般用于轻型或中型应用. 它的核心是控制反转(IoC)和面向切面(AOP). 主要优势是分层架构,允许选择使用哪一个组件.使用基本的Ja ...

  4. JAVA模拟Spring实现IoC过程(附源码)

    前言:本人大四学生,第一次写博客,如果有写得不好的地方,请大家多多指正 一.IoC(Inversion of Control)反转控制 传统开发都是需要对象就new,但这样做有几个问题: 效率低下,创 ...

  5. Redis(Windows安装方法与Java调用实例 & 配置文件参数说明 & Java使用Redis所用Jar包 & Redis与Memcached区别 & redis-cli.exe命令及示例)

    Windows下Redis的安装使用 0.前言 因为是初次使用,所以是在windows下进行安装和使用,参考了几篇博客,下面整理一下 1.安装Redis 官方网站:http://redis.io/ 官 ...

  6. Spring的Java配置方式—@Configuration和@Bean实现Java配置

    Java配置是Spring4.x推荐的配置方式,可以完全替代xml配置. 1.@Configuration 和 @BeanSpring的Java配置方式是通过 @Configuration 和 @Be ...

  7. Mybatis Spring multiple databases Java configuration

    https://stackoverflow.com/questions/18201075/mybatis-spring-multiple-databases-java-configuration ** ...

  8. 利用spring boot创建java app

    利用spring boot创建java app 背景 在使用spring框架开发的过程中,随着功能以及业务逻辑的日益复杂,应用伴随着大量的XML配置和复杂的bean依赖关系,特别是在使用mvc的时候各 ...

  9. spring+mybati java config配置引起的bean相互引用日志报警告问题

    摘要: Error creating bean with name 'XXX': Requested bean is currently in creation: Is there an unreso ...

随机推荐

  1. 当前主要的常用的PHP环境部署套件比较

    当前主要的常用的PHP环境部署套件比较 作为新手,需要学习PHP,或者需要搭建PHP+MySQL运行环境时,就需要去找各种搭建方法,一步一步按照操作流程操作,不仅繁琐,而且容易出错,还会带来安全隐患. ...

  2. iTOP-4418开发板最新资料及例程

    开发板资料: 开发板硬件资源: 1.POWER电源接口 电源输入为 5V/2A+,给核心板 AXP228 电源管理芯片提供 5V 电源. 2.SWITCH电源开关 轻触电源开关可以控制开发板电源通断. ...

  3. 迅为电子HMI人机界面|CAN总线触摸屏

    本文转自迅为:http://www.topeet.com 协议特色: 1. 支持所有 CAN 协议,例如常用的 J1939 和 CANopen 协议. 2. 提供高度开放的 CAN 帧的编辑界面,用户 ...

  4. cpio - 存取归档包中的文件

    总览 (SYNOPSIS) cpio {-o|--create} [-0acvABLV] [-C bytes] [-H format] [-M message] [-O [[user@]host:]a ...

  5. 双引号" "和单引号' '区别

    双引号是字符串,单引号是字符 “\n”与'\n': 相同点: 都能起到换行作用     不同点: "\n" <=> {'\n', '\0'} '\n' <=> ...

  6. thinkphp5将一条数据以toArray()放入session

    直接将找出的数据赋予变量,列:$admin: 存入session:Session::set('user_info',$admin->toArray()); 如果数据中有时间字段: 如若有时间字段 ...

  7. gulp 前缀

    var gulp = require('gulp'), autoprefixer = require('gulp-autoprefixer'); gulp.task('testAutoFx', fun ...

  8. jquery中ajax原生代码的分析模仿

    function ajax(obj){     var defaults = {         url: "#",         data: {},         type: ...

  9. 关于C/C++的一些思考(1)

    C++的前世今生: C的结构化思想: Ada的模版思想: Fortran的运算符重载思想: Simula的OO思想:封装,继承,多态: C++类型描述了变量的三个特征: 该类型在内存中占用物理空间的大 ...

  10. Insert 语句对 nologging 与 logging表 在不同场景下的优化

    前言 前段时间报表数据库上有条insert sql语句,插入的大量数据,执行非常慢,需要对其进行分析优化. 分析步骤是在:ARCHIVE与NOARCHIVE模式下进行. 测试场景: 分别对表的常规插入 ...