笔记-知识点部分:
applicationContext的主要实现类
给bean对象赋值
引入p名称空间约束
引入外部bean
配置内部bean
集合属性赋值
spring自动装配
引入 外部属性文件配置数据源(c空间)![](https://i-blog.csdnimg.cn/blog_migrate/98a785308a6b0cd1d62c3c9c612e43e2.png)
代码-实战部分
项目结构:
Book类:
package com.atguigu.spring.bean;
public class Book {
private Integer id;
private String name;
private String author;
private Double price;
private Integer amount;
public Book(Integer id, String name, String author, Integer amount) {
System.out.println("缺price构造器");
this.id = id;
this.name = name;
this.author = author;
this.amount = amount;
}
public Book(Integer id, String name, String author, Double price) {
System.out.println("缺amount构造器");
this.id = id;
this.name = name;
this.author = author;
this.price = price;
}
@Override
public String toString() {
return "Book{" +
"id=" + id +
", name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
", amount=" + amount +
'}';
}
public Book() {
}
public Book(Integer id, String name, String author, Double price, Integer amount) {
System.out.println("全参构造器");
this.id = id;
this.name = name;
this.author = author;
this.price = price;
this.amount = amount;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public Integer getAmount() {
return amount;
}
public void setAmount(Integer amount) {
this.amount = amount;
}
}
BookShop类:
package com.atguigu.spring.bean;
import java.util.List;
import java.util.Map;
public class BookShop {
List<Book> bookList;
Map<String,Book> usersMap;
@Override
public String toString() {
return "BookShop{" +
"bookList=" + bookList +
", usersMap=" + usersMap +
'}';
}
public BookShop() {
}
public BookShop(List<Book> bookList, Map<String, Book> usersMap) {
this.bookList = bookList;
this.usersMap = usersMap;
}
public List<Book> getBookList() {
return bookList;
}
public void setBookList(List<Book> bookList) {
this.bookList = bookList;
}
public Map<String, Book> getUsersMap() {
return usersMap;
}
public void setUsersMap(Map<String, Book> usersMap) {
this.usersMap = usersMap;
}
}
BookStore类:
package com.atguigu.spring.bean;
public class BookStore {
private Book book;
private int localX;
private int localY;
@Override
public String toString() {
return "BookStore{" +
"book=" + book +
", localX=" + localX +
", localY=" + localY +
'}';
}
public BookStore() {
}
public BookStore(Book book, int localX, int localY) {
this.book = book;
this.localX = localX;
this.localY = localY;
}
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
public int getLocalX() {
return localX;
}
public void setLocalX(int localX) {
this.localX = localX;
}
public int getLocalY() {
return localY;
}
public void setLocalY(int localY) {
this.localY = localY;
}
}
SpringTest类:
package com.atguigu.spring.test;
import com.atguigu.spring.bean.Book;
import com.atguigu.spring.bean.BookShop;
import com.atguigu.spring.bean.BookStore;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
public class SpringTest {
ApplicationContext ioc=new ClassPathXmlApplicationContext("applicationContext.xml");
/**
* 测试Book
*/
@Test
public void testBook(){
Book book1 = (Book)ioc.getBean("book5");
System.out.println("book1 = " + book1);
}
/**
* 测试BookStore
*/
@Test
public void testBookStore(){
BookStore bookStore =(BookStore) ioc.getBean("bookStore4");
System.out.println("bookStore = " + bookStore);
}
/**
* 测试BookShop
*/
@Test
public void testBookShop(){
BookShop bookShop1 =(BookShop) ioc.getBean("bookShop2");
System.out.println("bookShop1 = " + bookShop1);
}
/**
* 测试数据源
*/
@Test
public void testDataSource() throws SQLException {
DataSource dataSource=ioc.getBean("dataSource2",DataSource.class);
Connection connection = dataSource.getConnection();
System.out.println(connection);
}
}
applicationContext.xml文件(Spring配置核心内容):
<?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"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:c="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/util https://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- id是唯一标识,用于区分bean,class用于反射创建对象-->
<!-- setter方法赋值-->
<bean id="book1" class="com.atguigu.spring.bean.Book">
<!-- 给bean赋值-->
<property name="id" value="1"/>
<property name="name" value="西游记"/>
<property name="author" value="吴承恩"/>
<property name="price" value="38.99"/>
<property name="amount" value="30"/>
</bean>
<!-- 构造器赋值-->
<bean id="book2" class="com.atguigu.spring.bean.Book" name="book">
<!-- 可以通过index索引调整入参顺序,也可通过Type指定数据类型调整入参顺序-->
<constructor-arg index="0" value="2"/>
<constructor-arg index="2" value="施耐庵"/>
<constructor-arg index="1" value="水浒传"/>
<constructor-arg type="java.lang.Integer" value="100"/>
<constructor-arg type="java.lang.Double" value="88.88"/>
</bean>
<!-- 特殊值处理:CDATA区 -->
<bean id="book3" class="com.atguigu.spring.bean.Book">
<constructor-arg value="3"/>
<constructor-arg>
<value><![CDATA[<哈哈哈>]]></value>
</constructor-arg>
<constructor-arg value="王二"/>
<constructor-arg value="12.5"/>
<constructor-arg value="20"/>
</bean>
<!-- 构造器赋值,不能传null值,只能通过缺参构造器赋空值-->
<bean id="book4" class="com.atguigu.spring.bean.Book">
<property name="id" value="4"/>
<property name="name">
<value><![CDATA[<哈哈嘿嘿>]]></value>
</property>
<property name="author" value="张三"/>
<property name="price" value="39.5"/>
<property name="amount">
<null/>
</property>
</bean>
<!-- 使用p命名空间 此时就无法使用CDATA区,CDATA区必须在value标签中使用-->
<bean id="book5" class="com.atguigu.spring.bean.Book"
p:id="5"
p:name="一二三四五"
p:author="哈哈哈"
p:price="34.5"
p:amount="20"/>
<!-- 配置外部bean-->
<bean id="bookStore1" class="com.atguigu.spring.bean.BookStore">
<property name="book" ref="book5"/>
<property name="localX" value="5"/>
<property name="localY" value="4"/>
</bean>
<!-- 配置内部bean,无法引用-->
<bean id="bookStore2" class="com.atguigu.spring.bean.BookStore">
<property name="book">
<bean class="com.atguigu.spring.bean.Book"
p:id="6"
p:name="内部bean"
p:author="p空间"
p:price="28.88"
p:amount="200"/>
</property>
<property name="localX" value="7"/>
<property name="localY" value="7"/>
</bean>
<!-- 级联属性赋值-->
<bean id="bookStore3" class="com.atguigu.spring.bean.BookStore">
<property name="book" ref="book5"/>
<!-- 给级联属性赋值,通过name点属性名赋值-->
<property name="book.name" value="级联属性赋值"/>
<property name="localX" value="10"/>
<property name="localY" value="8"/>
</bean>
<!-- 集合属性赋值-->
<bean id="bookShop1" class="com.atguigu.spring.bean.BookShop">
<property name="bookList">
<!-- 集合属性list-->
<list>
<!-- 引用外部bean-->
<ref bean="book1"/>
<ref bean="book2"/>
<ref bean="book3"/>
<ref bean="book4"/>
<ref bean="book5"/>
<!-- 配置内部bean-->
<bean class="com.atguigu.spring.bean.Book"
p:id="7"
p:name="list内部bean"
p:author="p空间"
p:price="34.5"
p:amount="30"/>
</list>
</property>
<property name="usersMap">
<!-- 集合属性map-->
<map>
<entry key="张三" value-ref="book1"/>
<entry key="李四" value-ref="book2"/>
</map>
</property>
</bean>
<!-- 配置集合bean,util空间-->
<util:list id="bookList">
<ref bean="book1"/>
<ref bean="book2"/>
<ref bean="book3"/>
<ref bean="book4"/>
<ref bean="book5"/>
</util:list>
<util:map id="userMap">
<entry key="王五" value-ref="book5"/>
<entry key="赵六" value-ref="book4"/>
</util:map>
<bean id="bookShop2" class="com.atguigu.spring.bean.BookShop">
<!-- 引用集合空间-->
<property name="bookList" ref="bookList"/>
<property name="usersMap" ref="userMap"/>
</bean>
<!-- spring自动装配(注入)-->
<bean id="bookStore4" class="com.atguigu.spring.bean.BookStore" autowire="byName">
<property name="localX" value="20"/>
<property name="localY" value="20"/>
</bean>
<!-- 配置数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="username" value="root"/>
<property name="password" value="123456"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="initialSize" value="5"/>
<property name="maxActive" value="10"/>
</bean>
<!-- 引入外部属性文件配置数据源-->
<!-- 1.引入外部的属性文件-->
<c:property-placeholder location="classpath:druid.properties"/>
<!-- 配置数据源-->
<bean id="dataSource2" class="com.alibaba.druid.pool.DruidDataSource">
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="url" value="${jdbc.url}"/>
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="initialSize" value="${jdbc.initialSize}"/>
<property name="maxActive" value="${jdbc.maxActive}"/>
</bean>
</beans>
druid.properties文件(德鲁伊数据连接池配置文件):
jdbc.username=root
jdbc.password=123456
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.driverClassName=com.mysql.cj.jdbc.Driver
jdbc.initialSize=5
jdbc.maxActive=10