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

  • 相关阅读:
    Codeforces Round #336 (Div. 2) A. Saitama Destroys Hotel 模拟
    UVA 10341 二分搜索
    cscope 的使用
    2018年各大互联网前端面试题三(阿里)
    【前端统计图】hcharts实现堆叠柱形图(与后台数据交互)
    【前端统计图】echarts实现单条折线图
    【前端统计图】echarts实现简单柱状图
    css实现悬浮效果的阴影
    通过select下拉框里的value控制div显示与隐藏
    2018年各大互联网前端面试题四(美团)
  • 原文地址:https://www.cnblogs.com/Answer1215/p/9515394.html
Copyright © 2011-2022 走看看