zoukankan      html  css  js  c++  java
  • [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.

  • 相关阅读:
    Linux配置Java环境
    Oracle的flashback特性之一:Flashback Query
    Oracle的flashback特性之二:Flashback Table
    吴恩达深度学习笔记 (补)1.1~1.5 神经网络概述
    吴恩达深度学习笔记 2.10~2.18 向量化与python
    吴恩达深度学习笔记 2.6~2.9 logistic中的梯度下降
    吴恩达深度学习笔记 2.3 logistic回归损失
    吴恩达深度学习笔记2.2 logistic回归
    吴恩达深度学习笔记2.1 二分分类
    [ubuntu]安装并使用python 3.6及与2.7的切换
  • 原文地址:https://www.cnblogs.com/Answer1215/p/9539792.html
Copyright © 2011-2022 走看看