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. springMVC返回Base64位编码图片验证码

    import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.Graphics2D;impor ...

  2. ZooKeeper系列(四)

    一.配置服务 配置服务是分布式应用所需要的基本服务之一,它使集群中的机器可以共享配置信息中那些公共的部分.简单地说,ZooKeeper可以作为一个具有高可用性的配置存储器,允许分布式应用的参与者检索和 ...

  3. codeforces_D. Social Circles

    http://codeforces.com/contest/1060/problem/D 题意: n个客人,每个客人希望自己左边空li个座位,右边空ri个座位,可以形成任意个圆,问最少多少个座位. 思 ...

  4. HDU_3792_(素数筛+树状数组)

    Twin Prime Conjecture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  5. jquery 获取日期 date 对象、 判断闰年

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. C# 获取U盘ID序列号及U盘信息

    C# 获取U盘ID序列号及U盘信息 2011-05-20 上传大小:35KB c#VS2005U盘IDU盘信息   获取U盘ID序列号 VS2005编译通过,源码源自CSDN.已经测试好用. 可以获得 ...

  7. idea开启/关闭单词拼写检查

  8. C# Word 类库

    C# Word 类库 2009-08-06 22:10 14292人阅读 评论(11) 收藏 举报 c#objectstring文档microsoftexcel using System;using ...

  9. js 鼠标拖拽元素移动

    <!DOCTYPE html><html> <head> <title> </title> <style media="sc ...

  10. awk,grep,sed文本格式化处理

    一.awk取列 [root@web01 ~]# cat /etc/passwd|awk -F ':' '{print $1"\t\t"$7}' ###-F指定分隔符 root /b ...