package com.springtest.annobean;
public interface BookDao {
void add();
}
package com.springtest.annobean;
import org.springframework.stereotype.Repository;
@Repository
public class BookDaoImpl implements BookDao{
@Override
public void add() {
System.out.println("dao add ... ");
}
}
package com.springtest.annobean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class BookService {
/**
* 不再需要set方法,会自动set
*/
@Autowired
private BookDao bookDao;
public void test(){
System.out.println("service add ... ");
bookDao.add();
}
}
package com.springtest.annobean;
import org.springframework.stereotype.Service;
@Service(value = "userService") //等价于<bean id="userService" class="...">
public class UserService {
public void test(){
System.out.println("user service test ... ");
}
}
package com.springtest.autowire;
public class Dep {
@Override
public String toString() {
return "Dep{}";
}
}
package com.springtest.autowire;
public class Emp {
private Dep dep;
public void setDep(Dep dep) {
this.dep = dep;
}
public void test(){
System.out.println(dep);
}
}
package com.springtest.configclass;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.ComponentScan;
@Configurable //作为配置类,代替xml配置文件
@ComponentScan(basePackages = {"com.springtest.annobean"})
public class SpringConfig {
}
package com.springtest.outfile;
public class DbInfo {
private String className;
private String userName;
private String password;
public void setClassName(String className) {
this.className = className;
}
public void setUserName(String userName) {
this.userName = userName;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "DbInfo{" +
"className='" + className + '\'' +
", userName='" + userName + '\'' +
", password='" + password + '\'' +
'}';
}
}
package com.springtest;
/**
* 演示使用set方法进行注入属性
*/
public class Book {
//创建属性
private String bname;
private String bauthor;
//创建属性对应的set方法
public void setBname(String bname) {
this.bname = bname;
}
public void setBauthor(String bauthor) {
this.bauthor = bauthor;
}
public void testDemo() {
System.out.println(bname+"::"+bauthor);
}
}
package com.springtest;
public class BookCdata {
//创建属性
private String bname;
private String bauthor;
private String address;
//创建属性对应的set方法
public void setBname(String bname) {
this.bname = bname;
}
public void setBauthor(String bauthor) {
this.bauthor = bauthor;
}
public void setAddress(String address){
this.address = address;
}
public void testDemo() {
System.out.println(bname+"::"+bauthor+"::"+address);
}
}
package com.springtest;
import java.util.List;
public class BookList {
private List<String> books;
public void setBooks(List<String> books) {
this.books = books;
}
public void test(){
System.out.println(books);
}
}
package com.springtest;
public class BookNull {
//创建属性
private String bname;
private String bauthor;
private String address;
//创建属性对应的set方法
public void setBname(String bname) {
this.bname = bname;
}
public void setBauthor(String bauthor) {
this.bauthor = bauthor;
}
public void setAddress(String address){
this.address = address;
}
public void testDemo() {
System.out.println(bname+"::"+bauthor+"::"+address);
}
}
package com.springtest;
public class BookPname {
//创建属性
private String bname;
private String bauthor;
//创建属性对应的set方法
public void setBname(String bname) {
this.bname = bname;
}
public void setBauthor(String bauthor) {
this.bauthor = bauthor;
}
public void testDemo() {
System.out.println(bname+"::"+bauthor);
}
}
package com.springtest;
public class Course {
private String cname;
public void setCname(String cname) {
this.cname = cname;
}
@Override
public String toString() {
return "Course{" +
"cname='" + cname + '\'' +
'}';
}
}
package com.springtest;
public class Department {
private String dname;
public void setDname(String dname) {
this.dname = dname;
}
@Override
public String toString() {
return "Department{" +
"dname='" + dname + '\'' +
'}';
}
}
package com.springtest;
public class Department1 {
private String dname;
public void setDname(String dname) {
this.dname = dname;
}
@Override
public String toString() {
return "Department1{" +
"dname='" + dname + '\'' +
'}';
}
}
package com.springtest;
public class Employee {
private String ename;
private String gender;
//员工属于某一个部门,使用对象形式表示
private Department department;
public void setEname(String ename) {
this.ename = ename;
}
public void setGender(String gender) {
this.gender = gender;
}
public void setDepartment(Department department) {
this.department = department;
}
public void add() {
System.out.println(ename+"::"+gender+"::"+department);
}
}
package com.springtest;
public class Employee1 {
private String ename;
private String gender;
//员工属于某一个部门,使用对象形式表示
private Department1 department1;
public void setEname(String ename) {
this.ename = ename;
}
public void setGender(String gender) {
this.gender = gender;
}
public void setDepartment1(Department1 department1) {
this.department1 = department1;
}
public Department1 getDepartment1() {
return department1;
}
public void add() {
System.out.println(ename+"::"+gender+"::"+department1);
}
}
package com.springtest;
import org.springframework.beans.factory.FactoryBean;
public class MyBeanFactoryBean implements FactoryBean<Course> {
/**
* 定义类型为MyBeanFactoryBean
* 返回类型为Course
* @return
* @throws Exception
*/
@Override
public Course getObject() throws Exception {
Course course = new Course();
course.setCname("course name");
return course;
}
@Override
public Class<?> getObjectType() {
return null;
}
@Override
public boolean isSingleton() {
return false;
}
}
package com.springtest;
/**
* 使用有参数构造注入
*/
public class Orders {
//属性
private String oname="";
private String address;
//有参数构造
public Orders(String oname,String address) {
this.oname = oname;
this.address = address;
}
public void ordersTest() {
System.out.println(oname+"::"+address);
}
}
package com.springtest;
public class ScopeBean {
private String name;
public void setName(String name) {
this.name = name;
}
}
package com.springtest;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest {
/**
* only focus on use
* base on abstraction
*
*/
/**
* 参考自:
* https://www.bilibili.com/video/BV1Vf4y127N5
*
* spring:
* Spring框架是一个开放源代码的J2EE应用程序框架,由Rod Johnson发起,
* 是针对bean的生命周期进行管理的轻量级容器(lightweight container)。
* Spring解决了开发者在J2EE开发中遇到的许多常见的问题,提供了功能强大IOC、AOP及Web MVC等功能。
* Spring可以单独应用于构筑应用程序,也可以和Struts、Webwork、Tapestry等众多Web框架组合使用,
* 并且可以与 Swing等桌面应用程序AP组合。因此, Spring不仅仅能应用于J2EE应用程序之中,
* 也可以应用于桌面应用程序以及小应用程序之中。
* Spring框架主要由七部分组成,
* 分别是 Spring Core、 Spring AOP、 Spring ORM、 Spring DAO、Spring Context、 Spring Web和 Spring Web MVC。
*/
/**
* 两大核心
* IOC
* AOP
*
* 文档地址:
* https://docs.spring.io/spring-framework/docs/5.2.19.RELEASE/spring-framework-reference/
*/
/**
* 创建bean
* 参考自:
* https://docs.spring.io/spring-framework/docs/5.2.19.RELEASE/spring-framework-reference/core.html#spring-core
*/
@Test
public void test1(){
/**
* only focus on use
*/
//https://docs.spring.io/spring-framework/docs/5.2.19.RELEASE/spring-framework-reference/core.html#beans-factory-instantiation
ApplicationContext context = new ClassPathXmlApplicationContext("beans1.xml");
//https://docs.spring.io/spring-framework/docs/5.2.19.RELEASE/spring-framework-reference/core.html#beans-factory-client
User user = context.getBean("user", User.class);
user.add();
}
/**
* ioc的基本过程:
* 对象工厂
*
*/
/**
* 对象的处理:
* 使用xml配置文件创建对象
* <bean></bean>
*
* 参考:
* https://docs.spring.io/spring-framework/docs/5.2.19.RELEASE/spring-framework-reference/core.html#beans-dependencies
* 属性的处理:前提先创建对象
* DI,依赖注入,但是要在创建对象的基础上才可以进行(重点)
* **********先创建对象,然后再注入**********
* 方式:
* 1.使用property标签,完成属性的注入
* 2.使用构造函数注入,该构造函数为带参数的构造函数,使用constructor-arg
* https://docs.spring.io/spring-framework/docs/5.2.19.RELEASE/spring-framework-reference/core.html#beans-constructor-injection
* 3.
*
*
*/
@Test
public void test2(){
/**
* only focus on use
*/
/**
* set方式注入,即:property
*/
//https://docs.spring.io/spring-framework/docs/5.2.19.RELEASE/spring-framework-reference/core.html#beans-factory-instantiation
ApplicationContext context = new ClassPathXmlApplicationContext("beans1.xml");
//https://docs.spring.io/spring-framework/docs/5.2.19.RELEASE/spring-framework-reference/core.html#beans-factory-client
Book book = context.getBean("book", Book.class);
book.testDemo();
/**
* 构造函数注入
*/
Orders orders = context.getBean("orders", Orders.class);
orders.ordersTest();
/**
* p名称空间注入
* 参考自:
* https://docs.spring.io/spring-framework/docs/5.2.19.RELEASE/spring-framework-reference/core.html#beans-p-namespace
*
* 首先xml中引入命名空间
* xmlns:p="http://www.springframework.org/schema/p"
* 其次,配置xml
*
*/
BookPname bookPname = context.getBean("bookPname", BookPname.class);
bookPname.testDemo();
/**
* null值
* 参考自:
* https://docs.spring.io/spring-framework/docs/5.2.19.RELEASE/spring-framework-reference/core.html#beans-null-element
*
*/
BookNull bookNull = context.getBean("bookNull", BookNull.class);
bookNull.testDemo();
/**
* 特殊符号
* 1.使用转义
* 2.使用cdata
*/
BookCdata bookCdata = context.getBean("bookCdata", BookCdata.class);
bookCdata.testDemo();
/**
* 引入外部bean
* 应用场景:
* 一个service类,一个dao类
* service类调用dao类中的方法
*
* 从service的方法中new一个dao的类
* add(){
* UserDao dao = new UserDao();
* dao.insert();
* }
* 变成
* 将dao类变成service的属性(或者叫字段)
* private UserDao userDao;
*
*/
UserService userService = context.getBean("userService", UserService.class);
userService.add();
/**
* only focus on use
* base on abstraction
*/
/**
* 引入内部bean
* 应用场景:
* 一对多,多对一,多对多等关系
* 一个部门类,一个员工类
* 一个部门包含多个员工,一个员工只能属于某个部门
*
* 引用的同时,将引用的bean进行了定义
*/
Employee employee = context.getBean("employee", Employee.class);
employee.add();
/**
* 级联赋值
* 应用场景:
* 创建一个bean的时候,同时将引用的bean自动创建出来
*
*/
Employee1 employee1 = context.getBean("employee1", Employee1.class);
employee1.add();
/**
* 注入集合
* array
* list
* set
* map
* 对象list
*/
StudentCollect studentCollect = context.getBean("studentCollect", StudentCollect.class);
studentCollect.test();
/**
* 提取公共属性
* 引入命名空间
* xmlns:util="http://www.springframework.org/schema/util"
* 对应的scheme:
* http://www.springframework.org/schema/util
* http://www.springframework.org/schema/beans/spring-util.xsd
*
*/
}
}
package com.springtest;
import com.springtest.annobean.BookService;
import com.springtest.annobean.UserService;
import com.springtest.autowire.Emp;
import com.springtest.configclass.SpringConfig;
import com.springtest.outfile.DbInfo;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest2 {
/**
* only focus on use
* base on abstraction
*
*/
/**
* 参考自:
* https://www.bilibili.com/video/BV1Vf4y127N5
*
* spring:
* Spring框架是一个开放源代码的J2EE应用程序框架,由Rod Johnson发起,
* 是针对bean的生命周期进行管理的轻量级容器(lightweight container)。
* Spring解决了开发者在J2EE开发中遇到的许多常见的问题,提供了功能强大IOC、AOP及Web MVC等功能。
* Spring可以单独应用于构筑应用程序,也可以和Struts、Webwork、Tapestry等众多Web框架组合使用,
* 并且可以与 Swing等桌面应用程序AP组合。因此, Spring不仅仅能应用于J2EE应用程序之中,
* 也可以应用于桌面应用程序以及小应用程序之中。
* Spring框架主要由七部分组成,
* 分别是 Spring Core、 Spring AOP、 Spring ORM、 Spring DAO、Spring Context、 Spring Web和 Spring Web MVC。
*/
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans2.xml");
BookList books = context.getBean("bookList", BookList.class);
books.test();
}
/**
*
* only focus on use
* base on abstraction
*
* bean的类型:
* https://docs.spring.io/spring-framework/docs/5.2.19.RELEASE/spring-framework-reference/core.html#beans-factory-extension-factorybean
*
* 普通bean
* 工厂bean FactoryBean,它是个工厂
* 说明这个bean是个生产bean的工厂,而不是普通的bean
* 应用场景:
*
*/
@Test
public void test3(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans3.xml");
// MyBeanFactoryBean myBeanFactoryBean = context.getBean("myBeanFactoryBean", MyBeanFactoryBean.class);
// System.out.println(myBeanFactoryBean);
Course course = context.getBean("myBeanFactoryBean", Course.class);
System.out.println(course);
}
/**
* 作用域
* 单实例还是多实例
* 默认为单实例
* scope:singleton prototype request session
* singleton加载配置文件的时候就创建bean
* prototype加载配置文件时,不创建,调用getBean方法的时候,才创建
*/
@Test
public void test4(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans4.xml");
ScopeBean b1 = context.getBean("scopeBean", ScopeBean.class);
ScopeBean b2 = context.getBean("scopeBean", ScopeBean.class);
System.out.println(b1);
System.out.println(b2);
}
/**
* 声明周期
* 配置后置处理器
*/
/**
* 自动装配
* autowire值为:byName或者是byType
*/
@Test
public void test5(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans5.xml");
Emp emp = context.getBean("emp", Emp.class);
emp.test();
}
/**
* 引入外部属性文件
* 引入命名空间
* xmlns:context="http://www.springframework.org/schema/context"
* 以及对应的scheme
* http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
*
*/
@Test
public void test6(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans6.xml");
DbInfo dbInfo = context.getBean("dbInfo", DbInfo.class);
System.out.println(dbInfo);
}
/**
* 基于注解方式创建bean
* 依赖aop,因此要引入aop的pom
* 开启组件扫描,需要引入context的名称空间
* xmlns:context="http://www.springframework.org/schema/context"
* 以及对应的scheme
* http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
* @Controller
* @Service
* @Repository
* @Component
*
* only focus on use
* base on abstraction
* 只扫描部分类,如何实现?
* 配置filter来实现
*
* 注解对于属性,相当于xml中的<property ... ></property>
* @autowired 根据类型进行注入
* @qualifier 根据名称进行注入
* @resource 可以根据名称,也可以根据类型注入
* @value 普通类型字段的注入
*
*/
@Test
public void test7(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans7.xml");
UserService service = context.getBean("userService", UserService.class);
service.test();
//注解属性测试
BookService bookService = context.getBean("bookService", BookService.class);
bookService.test();
}
/**
* 完全注解方式,也就是使用配置类代替xml文件
* 如何实现?
* only focus on use
* base on abstraction
*
* @Configurable //作为配置类,代替xml配置文件
* @ComponentScan(basePackages = {"com.springtest.annobean"})
*/
@Test
public void test8(){
ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
UserService service = context.getBean("userService", UserService.class);
service.test();
//注解属性测试
BookService bookService = context.getBean("bookService", BookService.class);
bookService.test();
}
}
package com.springtest;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class StudentCollect {
//1 数组类型属性
private String[] courses;
//2 list集合类型属性
private List<String> list;
//3 map集合类型属性
private Map<String,String> maps;
//4 set集合类型属性
private Set<String> sets;
private List<Course> coursesList;
public void setCourses(String[] courses) {
this.courses = courses;
}
public void setList(List<String> list) {
this.list = list;
}
public void setMaps(Map<String, String> maps) {
this.maps = maps;
}
public void setSets(Set<String> sets) {
this.sets = sets;
}
public void setCoursesList(List<Course> coursesList) {
this.coursesList = coursesList;
}
public void test(){
System.out.println(Arrays.toString(courses));
System.out.println(list);
System.out.println(maps);
System.out.println(sets);
System.out.println(coursesList);
}
}
package com.springtest;
public class User {
public void add() {
System.out.println("add......");
}
}
package com.springtest;
public interface UserDao {
void add();
}
package com.springtest;
public class UserDaoImpl implements UserDao{
@Override
public void add() {
System.out.println("dao add ...... ");
}
}
package com.springtest;
public class UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void add(){
System.out.println("service add ....... ");
/**
* 将在方法中实例化引用的类
* 变成
* 将引用的类变成属性(字段),也就是private
*/
// UserDao userDao = new UserDaoImpl();
// userDao.add();
userDao.add();
}
}
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 参考自:
https://docs.spring.io/spring-framework/docs/5.2.19.RELEASE/spring-framework-reference/core.html#spring-core
-->
<bean id="user" class="com.springtest.User"></bean>
<!-- set方法注入属性-->
<bean id="book" class="com.springtest.Book">
<!--使用property完成属性注入
name:类里面属性名称
value:向属性注入的值
-->
<property name="bname" value="易筋经"></property>
<property name="bauthor" value="达摩老祖"></property>
</bean>
<bean id="orders" class="com.springtest.Orders">
<constructor-arg name="oname" value="电脑"></constructor-arg>
<constructor-arg name="address" value="China"></constructor-arg>
</bean>
<bean id="bookPname" class="com.springtest.BookPname" p:bname="九阳神功" p:bauthor="无名氏"></bean>
<!-- set方法注入属性-->
<bean id="bookNull" class="com.springtest.BookNull">
<!--使用property完成属性注入
name:类里面属性名称
value:向属性注入的值
-->
<property name="bname" value="易筋经"></property>
<property name="bauthor" value="达摩老祖"></property>
<property name="address">
<null></null>
</property>
</bean>
<bean id="bookCdata" class="com.springtest.BookCdata">
<!--使用property完成属性注入
name:类里面属性名称
value:向属性注入的值
-->
<property name="bname" value="易筋经"></property>
<property name="bauthor" value="达摩老祖"></property>
<property name="address">
<value><![CDATA[<<南京>>]]></value>
</property>
</bean>
<!-- 引用外部类 外部bean-->
<bean id="userService" class="com.springtest.UserService">
<!-- service中得添加set方法,否则无法注入,也就是会报错 -->
<property name="userDao" ref="userDaoImpl"></property>
</bean>
<bean id="userDaoImpl" class="com.springtest.UserDaoImpl"></bean>
<!-- 内部bean -->
<bean id="employee" class="com.springtest.Employee">
<property name="ename" value="lucy"></property>
<property name="gender" value="女"></property>
<property name="department">
<!-- 引用的同时,定义了bean -->
<bean id="department" class="com.springtest.Department">
<property name="dname" value="安保部"></property>
</bean>
</property>
</bean>
<!-- 级联赋值 方式1-->
<!--
<bean id="employee1" class="com.springtest.Employee1">
<property name="ename" value="lucy"></property>
<property name="gender" value="女"></property>
<property name="department1" ref="department1"></property>
</bean>
<bean id="department1" class="com.springtest.Department1">
<property name="dname" value="财务部"></property>
</bean>
-->
<!-- 级联赋值 方式2 employee1中字段department1需要开启get方法-->
<bean id="employee1" class="com.springtest.Employee1">
<property name="ename" value="lucy"></property>
<property name="gender" value="女"></property>
<property name="department1" ref="department1"></property>
<property name="department1.dname" value="技术部"></property>
</bean>
<bean id="department1" class="com.springtest.Department1"></bean>
<!-- 注入集合-->
<bean id="studentCollect" class="com.springtest.StudentCollect">
<!--数组类型属性注入-->
<property name="courses">
<array>
<value>java课程</value>
<value>数据库课程</value>
</array>
</property>
<!--list类型属性注入-->
<property name="list">
<list>
<value>张三</value>
<value>小三</value>
</list>
</property>
<!--map类型属性注入-->
<property name="maps">
<map>
<entry key="JAVA" value="java"></entry>
<entry key="PHP" value="php"></entry>
</map>
</property>
<!--set类型属性注入-->
<property name="sets">
<set>
<value>MySQL</value>
<value>Redis</value>
</set>
</property>
<!--属性为对象集合的注入-->
<property name="coursesList">
<list>
<ref bean="course1"></ref>
<ref bean="course2"></ref>
</list>
</property>
</bean>
<!--创建多个course对象-->
<bean id="course1" class="com.springtest.Course">
<property name="cname" value="Spring5框架"></property>
</bean>
<bean id="course2" class="com.springtest.Course">
<property name="cname" value="MyBatis框架"></property>
</bean>
</beans>
<?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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<!-- 参考自:
https://docs.spring.io/spring-framework/docs/5.2.19.RELEASE/spring-framework-reference/core.html#spring-core
-->
<util:list id="books">
<value>易筋经</value>
<value>九阴真经</value>
<value>九阳神功</value>
</util:list>
<bean id="bookList" class="com.springtest.BookList">
<property name="books" ref="books"></property>
</bean>
</beans>
<?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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<!-- 参考自:
https://docs.spring.io/spring-framework/docs/5.2.19.RELEASE/spring-framework-reference/core.html#spring-core
-->
<bean id="myBeanFactoryBean" class="com.springtest.MyBeanFactoryBean"></bean>
</beans>
<?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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<!-- 参考自:
https://docs.spring.io/spring-framework/docs/5.2.19.RELEASE/spring-framework-reference/core.html#spring-core
-->
<!--<bean id="scopeBean" class="com.springtest.ScopeBean"></bean>-->
<bean id="scopeBean" class="com.springtest.ScopeBean" scope="prototype"></bean>
</beans>
<?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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<!-- 参考自:
https://docs.spring.io/spring-framework/docs/5.2.19.RELEASE/spring-framework-reference/core.html#spring-core
-->
<!--<bean id="scopeBean" class="com.springtest.ScopeBean"></bean>-->
<bean id="emp" class="com.springtest.autowire.Emp" autowire="byName"></bean>
<bean id="dep" class="com.springtest.autowire.Dep"></bean>
</beans>
<?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">
<!-- 参考自:
https://docs.spring.io/spring-framework/docs/5.2.19.RELEASE/spring-framework-reference/core.html#spring-core
-->
<bean id="dbInfo" class="com.springtest.outfile.DbInfo">
<property name="className" value="${db.className}"></property>
<property name="userName" value="${db.userName}"></property>
<property name="password" value="${db.password}"></property>
</bean>
<context:property-placeholder location="classpath:db.properties"/>
</beans>
<?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">
<!-- 参考自:
https://docs.spring.io/spring-framework/docs/5.2.19.RELEASE/spring-framework-reference/core.html#spring-core
<context:component-scan base-package="org.example"/>
-->
<context:component-scan base-package="com.springtest.annobean"/>
</beans>
db.className=com.dbclass.name
db.userName=username
db.password=psd
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>sping5framework</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<outputDirectory>${basedir}/target/jar</outputDirectory>
<excludes>
<exclude>config/**</exclude>
<exclude>config_offline/**</exclude>
<exclude>config_online/**</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/jar/lib</outputDirectory>
<overWriteReleases>true</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<executions>
<!-- 替换会被 maven 特别处理的 default-compile -->
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
<!-- 替换会被 maven 特别处理的 default-testCompile -->
<execution>
<id>default-testCompile</id>
<phase>none</phase>
</execution>
<execution>
<id>java-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>java-test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.4.1.Final</version>
</extension>
</extensions>
</build>
</project>
对应github地址: