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. 学习嵌入式开发板的Android平台体系结构和源码结构

    本文转自迅为论坛资料:http://www.topeetboard.com 推荐学习嵌入式开发板平台:iTOP-4412开发板 下面这张图出自Google官方,展示了Android系统的主要组成部分. ...

  2. Python3.0 调用HTMLTestRunner生成的报告中不能显示用例中print函数的输出

    官方原生的HTMLTestRunner.py支持python2.0版本,python3.0版本的使用需要做一些修改: Python3调用HTMLTestRunner执行用例生成测试报告中,不能正常显示 ...

  3. 用cesium本身添加水纹效果

    参考网站:https://blog.csdn.net/XLSMN/article/details/78752669 1.首先来看一下整体效果 2.具体方法如下: 首先,你必须有两张很重要的图片,你可以 ...

  4. 模态对话框与非模态对话框(modeless)

    对话框有两种创建方式:DoModal和Creat. 其中DoModal创建的是模态的对话框,而Creat创建的是非模态的对话框下面总结下他们的不同. 对于模态的对话框,在该对话框被关闭前,用户将不能在 ...

  5. JAVA基础——IO流字符流

    字符流 字节流提供了处理任何类型输入/输出操作的功能(因为对于计算机而言,一切都是0和1,只需把数据以字节形式表示就够了),但它们不可以直接操作Unicode字符,因为上一篇文章写了,一个Unicod ...

  6. git命令初级

    git是开源的分布式版本控制系统,分布式主要区别于集中式代表CVS(Concurrent Version System,遵从C/S架构,同步比较笨拙.)和SVN(Subversion),linux开发 ...

  7. [LUOGU] P1113 杂物

    题目描述 John的农场在给奶牛挤奶前有很多杂务要完成,每一项杂务都需要一定的时间来完成它.比如:他们要将奶牛集合起来,将他们赶进牛棚,为奶牛清洗乳房以及一些其它工作.尽早将所有杂务完成是必要的,因为 ...

  8. mysql多表合并为一张表

    有人提出要将4张表合并成一张.数据量比较大,有4千万条数据.有很多重复数据,需要对某一列进行去重. 数据量太大的话,可以看我另外一篇:http://www.cnblogs.com/magmell/p/ ...

  9. [Python3网络爬虫开发实战] 1.8.3-Scrapy-Splash的安装

    Scrapy-Splash是一个Scrapy中支持JavaScript渲染的工具,本节来介绍它的安装方式. Scrapy-Splash的安装分为两部分.一个是Splash服务的安装,具体是通过Dock ...

  10. 树莓派 -- 输入设备驱动 (key) 续2: 转载 Setting up a GPIO-Button “keyboard” on a Raspberry Pi

    使用device-tree (DT) overlay应该是更方便的方法: http://blog.gegg.us/2017/01/setting-up-a-gpio-button-keyboard-o ...