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. 谈谈如何学习Linux操作系统

     献给初学者:为了能把这篇不错的文章分享给大家.所以请允许我暂时用原创的形式展现给大家. @hcy 更多资源:http://blog.sina.com.cn/iihcy 一. 选择适合自己的linux ...

  2. Bash Template

    #/bin/bash #set -x set -e usage() { cat <<EOF Usage: `basename $` [OPTIONS] <non-option arg ...

  3. reading from files

    如果有图会很好理解,最近太忙,以后再加吧 #首先有一个需要读取的文件名列表 #然后将文件名列表通过函数string_input_producer放进文件名队列. #有时候因为数据量太大,需要把他们放进 ...

  4. 2.10.1 article元素

    嵌套 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <titl ...

  5. ORACLE中的游标Cursor总结

    游标(Cursor):用来查询数据库,获取记录集合(结果集)的指针,可以让开发者一次访问一行结果集,在每条结果集上作操作. 游标可分为: 1.       静态游标:分为显式(explicit)游标和 ...

  6. ms_sql 触发器记录表字段数据变化的日志 -针对一张表操作

    create table sto (id int not null, -- 主键字段 de datetime -- 被跟踪的字段 constraint pk_sto primary key(id)) ...

  7. vue开发调试工具vue-devtools安装

    vue开发调试工具别人总结的非常好,所以直接把链接拿过来了,就当做个笔记了,也希望能帮到有需要的人,感谢“沉着前进”,来源(https://www.cnblogs.com/fighxp/p/78150 ...

  8. vue props传值方法

    <template> <div class="hello"> <ul> <li v-for="(item, index) in ...

  9. enote笔记法的思考(ver0.2)

    章节:enote笔记法的思考   enote笔记法,它是一种独特的文本标记方式与呈现方式.这一整套系统的记笔记的方法,它能够帮助我们对文本内容(例如,其中的概念.观点.思想等)更加直观和条理地进行理性 ...

  10. Centos6 安装nginx

    一.编译安装nginx 1.安装nginx所需要的库pcre,pcre的全称为:perl compatible regular expression即perl正则表达式,是为了使nginx具备URL重 ...