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. Apache与IIS端口冲突解决方法

    在安装Apache或者php集成环境包是经常会遇到Apache的80端口被占用导致无法正常启动Apache. Win7可以通过如下方法解决(如果坚持要使用80端口的话): 1.打开"控制面板 ...

  2. ubuntu命令行使用ftp客户端

    转载 本篇文章主要介绍在Ubuntu 8.10下如何使用功能强大的FTP客户端软件NcFTP. Ubuntu的源里为我们提供了FTP客户端软件NcFTP,可这款工具对新手来说不是很方便.本文介绍的是一 ...

  3. C# GDI+ 画坐标(x,y)

    private void button1_Click(object sender, EventArgs e) { Graphics g = this.CreateGraphics(); g.Clear ...

  4. LPCTSTR 字符串获取其长度

    LPCTSTR lpStr = "123456789";int i=CString(lpStr).GetLength();

  5. js 异步提交文件

    <form method="POST" action="${ctx}/statement/manage/upload" name="form&q ...

  6. Oracle 学习之:ASCII,CHR函数的作用和用法

    对于ASCII以及CHR函数的用法,Oracle给出的解释是: ASCII(x)gets the ASCII value of the character X, CHR() and ASCII() h ...

  7. [Python3网络爬虫开发实战] 1.4.1-MySQL的安装

    MySQL是一个轻量级的关系型数据库,本节中我们来了解下它的安装方式. 1. 相关链接 官方网站:https://www.mysql.com/cn 下载地址:https://www.mysql.com ...

  8. 前端常用的js 插件合集

  9. 新进Linux菜鸟,请多多关照

    早早知晓Linux的大名,一直未研究学习,近来看了kernel一些源代码,在网上搜过很多基础的知识.感觉这个Linux的世界很广大,值得好好深入学习.初生婴儿,呱呱落地,必先躺若干日后能坐,在学爬,进 ...

  10. LeetCode(64) Minimum Path Sum

    题目 Total Accepted: 47928 Total Submissions: 148011 Difficulty: Medium Given a m x n grid filled with ...