博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Java Sprint] Spring Configuration Using Java
阅读量:5281 次
发布时间:2019-06-14

本文共 4247 字,大约阅读时间需要 14 分钟。

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;@Configurationpublic 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
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;@Configurationpublic 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
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.

 

转载于:https://www.cnblogs.com/Answer1215/p/9539792.html

你可能感兴趣的文章
ftp与/usr/bin/ftp
查看>>
git创建版本库以及使用
查看>>
ANF框架小结、网络概念步骤详情及开发源码
查看>>
【模板】可持久化线段树 1(主席树)
查看>>
动态规划 洛谷P2285 [HNOI2004] 打鼹鼠
查看>>
用MPMoviePlayerController做在线音乐播放
查看>>
maven的安装
查看>>
poj 1684 Lazy Math Instructor(字符串)
查看>>
20172322 2017-2018-2《程序设计与数据结构》(上)课程总结
查看>>
什么是EDID,EDID能做什么,EDID基本介绍
查看>>
JavaScript面向对象
查看>>
matlab中生成随机数的相关知识
查看>>
let与const心智模型
查看>>
1009. Product of Polynomials (25)
查看>>
【dp 背包变形】 poj 1837
查看>>
面向对象设计原则之里氏代换原则【转载】
查看>>
iOS 开发者必不可少的 75 个工具,你都会了吗
查看>>
Hdu 1029 Ignatius and the Princess IV
查看>>
走进AngularJs(一)angular基本概念的认识与实战
查看>>
以STM32和FPGA为核心的多组件协调工作系统
查看>>