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. asterisk-java ami3 属性改变监听

    asteriskServer.addAsteriskServerListener(new AsteriskListenerInit());//服务属性监听会自动连接服务 实现AsteriskServe ...

  2. 核武器代理CC工具V3.42最新版本!

    软件说明 !!!有新版本更新,请移步到更新地址:https://www.cnblogs.com/cnhacker/p/10878688.html ########################### ...

  3. Python3.0 操作MySQL数据库执行SQL语句

    py3不支持MySQLdb,需要导入pymysql模块 # coding: utf-8 # Team : Quality Management Center # Author:Carson # Dat ...

  4. 使用WinPcap编程

    创建一个使用 wpcap.dll 的应用程序 用 Microsoft Visual C++ 创建一个使用 wpcap.dll 的应用程序,需要按一下步骤: 在每一个使用了库的源程序中,将 pcap.h ...

  5. java.lang.NoClassDefFoundError: org.springframework.beans.FatalBeanException

    在进行Spring和Hibernate整合的时候遇到了这个问题, 问题描述如下 问题原因?  Spring的Bean的XML配置文件存在错误 解决方法: 正确的配置XML文件,例如下面的代码 < ...

  6. print keys %map_function 输出 散列的值: OK_funcsplit_funcpackage_VAR

    my %map_function = (     88     "OK_func" => "open_statement",     89     &qu ...

  7. python 弹窗

    import ctypes message = ctypes.windll.user32.MessageBoxA(0,'message','tips',0)

  8. js正则表达式,只允许输入纯数字或‘/’

    //输入框,限数字和/----需要多个数量询价,请以/分隔 function onlyonce(obj) {//先把非数字的都替换掉,除了数字和.obj.value = obj.value.repla ...

  9. BZOJ 4519 不同的最小割 最小割树

    题面: 把每两个点当成源汇,求N*(N-1)个最小割中不同的有多少个 N<=850 分析: 有这样一个结论:一张无向图不同的最小割最多有n-1个. 那么我们一定可以建出一棵树,使得这棵树中每两个 ...

  10. jq ajax请求error: Maximum call stack size exceeded

    原因是data中参数iconUrl这个变量未声明导致的.jq在内部循环时报错