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

[Java Spring] Spring Annotation Configuration Using XML的更多相关文章

  1. 如何用Java类配置Spring MVC(不通过web.xml和XML方式)

    DispatcherServlet是Spring MVC的核心,按照传统方式, 需要把它配置到web.xml中. 我个人比较不喜欢XML配置方式, XML看起来太累, 冗长繁琐. 还好借助于Servl ...

  2. Spring 4 Ehcache Configuration Example with @Cacheable Annotation

    http://www.concretepage.com/spring-4/spring-4-ehcache-configuration-example-with-cacheable-annotatio ...

  3. Java框架spring Boot学习笔记(七):@Configuration,@bean注解

    @Configuration作用在类上,相当于一个xml文件 @bean作用于方法上,相当于xml配置中的<bean>标签 一个例子: 新建一个Springboot工程 新建一个User类 ...

  4. SpringBoot application.yml logback.xml,多环境配置,支持 java -jar --spring.profiles.active

    趁今天有时间整理了一下 启动命令为 //开发环境 java -jar app.jar --spring.profiles.active=dev--server.port=8060 //测试环境 jav ...

  5. java 9 Spring Cloud @EnableEurekaServer javax.xml.bind.JAXBContext not present

    java 9 Spring Cloud @EnableEurekaServer   javax.xml.bind.JAXBContext not present jdk 8下面还可以正常启动,jdk9 ...

  6. SpringBoot application.yml logback.xml,多环境配置,支持 java -jar --spring.profiles.active(转)

    趁今天有时间整理了一下 启动命令为 //开发环境 java -jar app.jar --spring.profiles.active=dev--server.port=8060 //测试环境 jav ...

  7. MongoDB和Java(4):Spring Data整合MongoDB(XML配置)

    最近花了一些时间学习了下MongoDB数据库,感觉还是比较全面系统的,涉及了软件安装.客户端操作.安全认证.副本集和分布式集群搭建,以及使用Spring Data连接MongoDB进行数据操作,收获很 ...

  8. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring基于Annotation装配Bean

    在 Spring 中,尽管使用 XML 配置文件可以实现 Bean 的装配工作,但如果应用中 Bean 的数量较多,会导致 XML 配置文件过于臃肿,从而给维护和升级带来一定的困难. Java 从 J ...

  9. Java框架spring 学习笔记(十八):事务管理(xml配置文件管理)

    在Java框架spring 学习笔记(十八):事务操作中,有一个问题: package cn.service; import cn.dao.OrderDao; public class OrderSe ...

随机推荐

  1. CentOS7 Install Consul

    Centos7 Install Consul 原文链接:http://www.cnblogs.com/caoguo/p/5959962.html 1) 环境 2) 安装 # yum install - ...

  2. Maven error in eclipse (pom.xml) : Failure to transfer org.apache.maven.plugins:maven-surefire-plugin:pom:2.12.4

    i wanna make web project using the Maven to import automatically all libraries that i need, so i cho ...

  3. CAD交互绘制带周长面积的矩形框(com接口)

    主要用到函数说明: _DMxDrawX::DrawLine 绘制一个直线.详细说明如下: 参数 说明 DOUBLE dX1 直线的开始点x坐标 DOUBLE dY1 直线的开始点y坐标 DOUBLE ...

  4. JSP的九大对象和四大作用域

    1.JSP中九大内置对象为: request            请求对象          类型 javax.servlet.ServletRequest        作用域   Request ...

  5. 2.10.3 nav 元素

    nav <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <tit ...

  6. SpringMVC+ajax返回JSON串

    一.引言 本文使用springMVC和ajax做的一个小小的demo,实现将JSON对象返回到页面,没有什么技术含量,纯粹是因为最近项目中引入了springMVC框架. 二.入门例子 ①. 建立工程, ...

  7. live555简介

    live555 编辑   目录 1live555简介 2Live555 Streaming Media整体框架 3openRTSP客户端流程     1live555简介编辑 Live555 是一个为 ...

  8. PHP实现微信第三方登录的方法

    本文实例讲述了PHP版微信第三方实现一键登录及获取用户信息的方法.分享给大家供大家参考,具体如下: 注意,要使用微信在第三方网页登录是需要“服务号”才可以哦,所以必须到官方申请 一开始你需要进入微信公 ...

  9. centos6 用户登陆管理

    查看当前登陆有哪些用户,在做什么 [root@web01 ~]# w :: up :, users, load average: 0.00, 0.00, 0.00 USER TTY FROM LOGI ...

  10. AutoEncoders变种

    目录 PCA V.S. Auto-Encoders Denoising AutoEncoders Dropout AutoEncoders PCA V.S. Auto-Encoders deep au ...