[Java Sprint] Spring Configuration Using Java

博客指出无applicationContext.xml文件且XML过多,引入Java配置。介绍了创建AppConfig.java文件,包含Setter注入、构造函数注入和Autowired注入方式,还提到在Java文件中添加@Service和@Repository,在AppConfig.java添加@ComponentScan,Autowired可免在AppConfig定义@Bean。

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.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值