zoukankan      html  css  js  c++  java
  • [Java Spring] Spring Annotation Configuration Using XML

    Add context to our application.

    main/resources/applicationContext.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd">
    
        <context:annotation-config />
    
        <context:component-scan base-package="com.pluralsight" />
    </beans>

    Using annotation for @Service @Repository @Controller:

    /repository/HibernateCustomerRepositoryImpl.java:

    package com.pluralsight.repository;
    
    import com.pluralsight.model.Customer;
    
    import org.springframework.stereotype.Repository;
    
    import java.util.ArrayList;
    import java.util.List;
    
    @Repository("customerRepository")
    public class HibernateCustomerRepositoryImpl implements CustomerRepository {
    
        @Override
        public List<Customer> findAll() {
            List<Customer> customers = new ArrayList<>();
            Customer customer = new Customer();
    
            customer.setFirstname("Byran");
            customer.setLastname("John");
    
            customers.add(customer);
    
            return customers;
        }
    }

    /service/CustomerServiceImpl.java:

    We use @Autowrie for the member of Repository.

    package com.pluralsight.service;
    
    import com.pluralsight.model.Customer;
    import com.pluralsight.repository.CustomerRepository;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import java.util.List;
    
    @Service("customerService")
    public class CustomerServiceImpl implements CustomerService {
    
        @Autowired
        private CustomerRepository customerRepository;
    
        @Override
        public List<Customer> findAll() {
            return customerRepository.findAll();
        }
    
    }

    We can also use Setter Injection:

        private CustomerRepository customerRepository;
    
        @Autowired
        public CustomerServiceImpl (CustomerRepository customerRepository) {
            this.customerRepository = customerRepository;
        }

    We can also use Constructor Injection:

        private CustomerRepository customerRepository;
    
        @Autowired
        public CustomerServiceImpl (CustomerRepository customerRepository) {
            this.customerRepository = customerRepository;
        }

    After using Annotations & Autowired, we code looks simple and decoupled

  • 相关阅读:
    在Centos 7下编译openwrt+njit-client
    开博随笔
    Chapter 6. Statements
    Chapter 4. Arrays and Pointers
    Chapter 3. Library Types
    Chapter 2.  Variables and Basic Types
    关于stm32不常用的中断,如何添加, 比如timer10 timer11等
    keil 报错 expected an identifier
    案例分析 串口的地不要接到电源上 会烧掉
    案例分析 CAN OPEN 调试记录 进度
  • 原文地址:https://www.cnblogs.com/Answer1215/p/9515394.html
Copyright © 2011-2022 走看看