对于spring的核心容器的理解(黑马SSM)
文章目录
对于我们spring的核心容器主要分三个部分:
我们这里创建的模块都是普通的java模块创建,只有在使用spring的容器时才会使用spring的依赖
创建方法:
首先找到文件,找到新建,点击新建模块,这样来创建模块
然后找到新建模块进行创建
注意:我们这里的依赖管理选择的是maven,并且我们的模块位置不能放在其他模块里面
引入spring的依赖,我们在模块目录找到pom.xml文件写我们的依赖
代码(我们的这个依赖就是核心容器的主要依赖):
<dependencies>
<!--导入spring的坐标spring-context-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.1.12</version>
</dependency>
</dependencies>
然后我们在模块目录找到resources创建我们的application.xml文件,这样我们就可以开始学习bean了
相关的类和文件,我们在模块目录找到我们的com.itheima这个包,进行创建
创建dao这个包
创建dao的接口代码:
package com.itheima.dao;
public interface BookDao {
public void save();
}
创建dao实现类代码:
我们引入接口之后,它就会提示重写父方法,如果我们需要自动实现,把我们的鼠标放在红线上,Alt和Enter同时按就可以自动重写父类方法
代码:
package com.itheima.dao.impl;
import com.itheima.dao.BookDao;
public class BookDaoImpl implements BookDao {
@Override
public void save() {
System.out.println("book dao save ...");
}
}
创建service类
创建service的接口代码:
package com.itheima.service;
public interface BookService {
public void save();
}
创建service实现类代码:
package com.itheima.service.impl;
import com.itheima.service.BookService;
public class BookServiceImpl implements BookService {
@Override
public void save() {
System.out.println("book service save ...");
}
}
容器相关:
BeanFactory:
我们的BeanFactory是IoC容器的顶层接口,是spring的最初始的创建IoC容器的接口,它在初始化BeanFactory对象时候,会延迟加载我们bean,只有等我们获取我们需要的bean的时候才会加载
模块结构:
代码:
package com.itheima;
import com.itheima.dao.BookDao;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class Main_beanfactory {
public static void main(String[] args) {
//beanfactory读取配置文件之后不会立即创建bean容器,会延迟加载
Resource resources=new ClassPathResource("application.xml");
BeanFactory beanFactory=new XmlBeanFactory(resources);
BookDao bean = beanFactory.getBean(BookDao.class);
bean.save();
}
}
结果:
注意:
如果我们的new XmlBeanFactory(resources)方法报错可能因为这个方法太老了被淘汰了,我们只需要了解就行
ApplicationContext:
我们的ApplicationContext是继承了我们接口BeanFactory,实现了里面的方法,如果我们想看是否真的继承
首先按住Ctrl然后把鼠标移到我们的BeanFactory,点击进入
进入后,同时按住Ctrl和H键,就会出现层次结构
FileSystemXmlApplicationContext:
FileSystemXmlApplicationContext这个类也是继承了ApplicationContext这个接口,作用主要是加载相对位置的配置文件,如果要加载多个配置文件可以通过逗号来就行分隔,相对路径更加推荐
ClassPathXmlApplicationContext:
ClassPathXmlApplicationContext这个类也是继承了ApplicationContext这个接口,作用主要是加载绝对位置的配置文件,如果要加载多个配置文件可以通过逗号来就行分隔,绝对路径来加载并不推荐,因为电脑不同绝对路径也就不同
Bean相关:
Bean的创建:
在application.xml里面创建相关的Bean
id指我们Bean的名字
class指我们Bean的类型
代码:
<bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl"/>
Bean的属性
name指给bean取别名
目录:
bean创建代码:
<bean id="bookDao" name="dao" class="com.itheima.dao.impl.BookDaoImpl"/>
测试代码:
package com.itheima;
import com.itheima.dao.impl.BookDaoImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
BookDaoImpl bookDao1 = (BookDaoImpl) applicationContext.getBean("bookDao");
BookDaoImpl bookDao2 = (BookDaoImpl) applicationContext.getBean("dao");
bookDao1.save();
bookDao2.save();
}
}
结果:
scope指控制创建bean数量
目录:
bean创建代码:
<bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl" scope="prototype"/>
测试代码:
package com.itheima;
import com.itheima.dao.impl.BookDaoImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
BookDaoImpl bookDao1 = (BookDaoImpl) applicationContext.getBean("bookDao");
BookDaoImpl bookDao2 = (BookDaoImpl) applicationContext.getBean("bookDao");
System.out.println(bookDao1);
System.out.println(bookDao2);
}
}
结果:
加上scope之前,地址相同代表只创建了一个bean
加上scope之后,地址不同代表了创建的多个bean
init-method指初bean始化方法:
目录:
实现dao类代码:
package com.itheima.dao.impl;
import com.itheima.dao.BookDao;
public class BookDaoImpl implements BookDao {
@Override
public void save()
{
System.out.println("book dao save ...");
}
public void init(){
System.out.println("init...");
}
}
bean创建代码:
<bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl" init-method="init"/>
测试代码:
package com.itheima;
import com.itheima.dao.impl.BookDaoImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
BookDaoImpl bookDao1 = (BookDaoImpl) applicationContext.getBean("bookDao");
bookDao1.save();
}
}
结果:
destory-method指bean销毁方法
目录:
实现dao类代码:
package com.itheima.dao.impl;
import com.itheima.dao.BookDao;
public class BookDaoImpl implements BookDao {
@Override
public void save()
{
System.out.println("book dao save ...");
}
public void destroy(){
System.out.println("destroy...");
}
}
bean创建代码:
<bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl" destroy-method="destroy"/>
测试代码:
package com.itheima;
import com.itheima.dao.impl.BookDaoImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
BookDaoImpl bookDao1 = (BookDaoImpl) applicationContext.getBean("bookDao");
bookDao1.save();
}
}
结果:
我们发现我们的销毁方法并没有被调用,那是因为我们的程序在虚拟机上面运行,当我们的虚拟机关闭的时候,我们的bean的销毁方法来不及调用,我们的IoC容器就已经被销毁了,所以我们才看不到,解决这个问题,我们需要调用方法close()这个方法(继承于ConfigurableApplicationContext,但是我们的ConfigurableApplicationContext继承于ApplicationContext),给虚拟机做一个标记,告诉虚拟机关闭之前要关闭我们的bean容器
具体找寻方法跟ApplicationContext寻找方法相同
修改后的测试代码:
package com.itheima;
import com.itheima.dao.impl.BookDaoImpl;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
BookDaoImpl bookDao1 = (BookDaoImpl) applicationContext.getBean("bookDao");
bookDao1.save();
applicationContext.close();
}
}
修改后的结果:
factory-method用于静态工厂实例化bean
实现OrderDaoFactory类代码:
package com.itheima.factory;
import com.itheima.dao.BookDao;
import com.itheima.dao.impl.BookDaoImpl;
public class OrderDaoFactory {
public static BookDao BookDao(){
System.out.println("factory....");
return new BookDaoImpl();
}
}
bean创建代码:
<bean id="bookDao" class="com.itheima.factory.OrderDaoFactory" factory-method="BookDao"/>
结果:
factory-bean用于实例化工厂实例化bean,搭配factory-method一起使用
实现OrderDaoFactory类代码:
package com.itheima.factory;
import com.itheima.dao.BookDao;
import com.itheima.dao.impl.BookDaoImpl;
public class OrderDaoFactory {
public BookDao BookDao(){
System.out.println("factory....");
return new BookDaoImpl();
}
}
bean创建代码:
<bean id="orderDaoFactory" class="com.itheima.factory.OrderDaoFactory"/>
<bean id="bookDao" factory-method="BookDao" factory-bean="orderDaoFactory"/>
结果:
lazy-init指延迟加载
目录:
实现dao类代码:
package com.itheima.dao.impl;
import com.itheima.dao.BookDao;
public class BookDaoImpl implements BookDao {
@Override
public void save()
{
System.out.println("book dao save ...");
}
public BookDaoImpl() {
System.out.println("book dao constructor...");
}
}
在我们IoC容器被获取后,容器里面的构造方法也会执行
测试代码:
package com.itheima;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
}
}
bean创建代码(加lazy-init之前):
<bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl"/>
结果:
bean创建代码(加lazy-init之后):
<bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl" lazy-init="true"/>
结果:
依赖注入相关:
依赖注入方式:
构造方法进行注入(适用于第三方类):
如果我们想自动创建构造方法,我们需要在该类当中同时按住Alt和Insert两个键,这样我们就可以自动创建构造方法了
注入类型:
简单类型:
目录:
实现dao类代码:
package com.itheima.dao.impl;
import com.itheima.dao.BookDao;
public class BookDaoImpl implements BookDao {
private int num;
public BookDaoImpl(int num) {
this.num = num;
}
@Override
public void save()
{
System.out.println("book dao save ..."+this.num);
}
}
bean创建代码:
注意:我们的constructor-arg的name对应的是我们构造方法的形参,同时我们的name可以由其他的替代
形参的类型来注入:type(但是如果形参类型相同,则无法注入)
形参的位置来注入:index
<bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl">
<constructor-arg name="num" value="10"/>
</bean>
测试代码:
package com.itheima;
import com.itheima.dao.impl.BookDaoImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
BookDaoImpl bookDao = (BookDaoImpl) applicationContext.getBean("bookDao");
bookDao.save();
}
}
结果:
复杂类型:
我们使用的类型是自己创建复杂类型
目录:
实现dao类代码:
package com.itheima.dao.impl;
import com.itheima.dao.BookDao;
public class BookDaoImpl implements BookDao {
@Override
public void save()
{
System.out.println("book dao save ...");
}
}
实现service类代码:
package com.itheima.service.impl;
import com.itheima.dao.impl.BookDaoImpl;
import com.itheima.service.BookService;
public class BookServiceImpl implements BookService {
public BookDaoImpl bookDaoimpl;
public BookServiceImpl(BookDaoImpl bookDaoimpl) {
this.bookDaoimpl = bookDaoimpl;
}
@Override
public void save() {
System.out.println("book service save ...");
bookDaoimpl.save();
}
}
bean创建代码:
注意:我们的constructor-arg的name对应的是我们构造方法的形参
<bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl"/>
<bean id="bookService" class="com.itheima.service.impl.BookServiceImpl">
<constructor-arg name="bookDaoimpl" ref="bookDao"/>
</bean>
测试代码:
package com.itheima;
import com.itheima.service.impl.BookServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
BookServiceImpl bookService = (BookServiceImpl) applicationContext.getBean("bookService");
bookService.save();
}
}
结果:
集合简单类型:
目录:
实现dao类代码:
package com.itheima.dao.impl;
import com.itheima.dao.BookDao;
import java.util.List;
public class BookDaoImpl implements BookDao {
private List<Integer> list;
public BookDaoImpl(List<Integer> list) {
this.list = list;
}
@Override
public void save()
{
System.out.println(list);
System.out.println("book dao save ...");
}
}
bean创建代码:
注意:我们的constructor-arg的name对应的是我们构造方法的形参
<bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl">
<constructor-arg name="list">
<list>
<value>1</value>
<value>2</value>
</list>
</constructor-arg>
</bean>
测试代码:
package com.itheima;
import com.itheima.dao.impl.BookDaoImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
BookDaoImpl bookDao = (BookDaoImpl) applicationContext.getBean("bookDao");
bookDao.save();
}
}
结果:
集合复杂类型:
我们使用的类型是自己创建复杂类型
目录:
实现dao类代码:
package com.itheima.dao.impl;
import com.itheima.dao.BookDao;
public class BookDaoImpl implements BookDao {
@Override
public void save()
{
System.out.println("book dao save ...");
}
}
实现service代码:
package com.itheima.service.impl;
import com.itheima.dao.impl.BookDaoImpl;
import com.itheima.service.BookService;
import java.util.List;
public class BookServiceImpl implements BookService {
public List<BookDaoImpl> bookDaoList;
public BookServiceImpl(List<BookDaoImpl> bookDaoList) {
this.bookDaoList = bookDaoList;
}
@Override
public void save() {
System.out.println(bookDaoList);
System.out.println("book service save ...");
}
}
bean创建代码:
因为我们这里是复杂类型的集合,所以我们要设置我们的dao这个bean为
多个bean创建
注意:我们的constructor-arg的name对应的是我们构造方法的形参
<bean id="bookDao" scope="prototype" class="com.itheima.dao.impl.BookDaoImpl"/>
<bean id="bookService" class="com.itheima.service.impl.BookServiceImpl">
<constructor-arg name="bookDaoList">
<list>
<ref bean="bookDao"/>
<ref bean="bookDao"/>
</list>
</constructor-arg>
</bean>
结果:
setter方法进行注入(适用于自己创建的类):
注入类型:
简单类型:
目录:
实现dao类代码:
package com.itheima.dao.impl;
import com.itheima.dao.BookDao;
public class BookDaoImpl implements BookDao {
private int num;
public void setNum(int num){
this.num = num;
}
@Override
public void save()
{
System.out.println("book dao save ..."+this.num);
}
}
bean创建代码:
<bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl">
<property name="num" value="10"/>
</bean>
测试代码:
package com.itheima;
import com.itheima.dao.impl.BookDaoImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
BookDaoImpl bookDao = (BookDaoImpl) applicationContext.getBean("bookDao");
bookDao.save();
}
}
结果:
复杂类型:
我们使用的类型是自己创建复杂类型
目录:
实现dao类代码:
package com.itheima.dao.impl;
import com.itheima.dao.BookDao;
public class BookDaoImpl implements BookDao {
@Override
public void save()
{
System.out.println("book dao save ...");
}
}
实现service类代码:
package com.itheima.service.impl;
import com.itheima.dao.impl.BookDaoImpl;
import com.itheima.service.BookService;
public class BookServiceImpl implements BookService {
public BookDaoImpl bookDaoimpl;
public void setBookDaoimpl(BookDaoImpl bookDaoimpl) {
this.bookDaoimpl = bookDaoimpl;
}
@Override
public void save() {
System.out.println("book service save ...");
bookDaoimpl.save();
}
}
bean创建代码:
注意:property的name要跟setter方法名字setBookDaoimpl后面的后缀一样,否则会报错,ref指bean的id
<bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl"/>
<bean id="bookService" class="com.itheima.service.impl.BookServiceImpl">
<property name="BookDaoimpl" ref="bookDao"/>
</bean>
测试代码:
package com.itheima;
import com.itheima.service.impl.BookServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
BookServiceImpl bookService = (BookServiceImpl) applicationContext.getBean("bookService");
bookService.save();
}
}
结果:
集合简单类型:
目录:
实现dao类代码:
package com.itheima.dao.impl;
import com.itheima.dao.BookDao;
import java.util.List;
public class BookDaoImpl implements BookDao {
private List<Integer> list;
public void setList(List<Integer> list){
this.list = list;
}
@Override
public void save()
{
System.out.println(list);
System.out.println("book dao save ...");
}
}
bean创建代码:
注意:property的name要跟setter方法名字setBookDaoimpl后面的后缀一样,否则会报错,ref指bean的id
<bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl">
<property name="list">
<list>
<value>1</value>
<value>2</value>
</list>
</property>
</bean>
测试代码:
package com.itheima;
import com.itheima.dao.impl.BookDaoImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
BookDaoImpl bookDao = (BookDaoImpl) applicationContext.getBean("bookDao");
bookDao.save();
}
}
结果:
集合复杂类型:
我们使用的类型是自己创建复杂类型目录:
实现dao类代码:
package com.itheima.dao.impl;
import com.itheima.dao.BookDao;
public class BookDaoImpl implements BookDao {
@Override
public void save()
{
System.out.println("book dao save ...");
}
}
实现service类代码:
package com.itheima.service.impl;
import com.itheima.dao.impl.BookDaoImpl;
import com.itheima.service.BookService;
import java.util.List;
public class BookServiceImpl implements BookService {
public List<BookDaoImpl> bookDaoList;
public void setBookServiceImpl(List<BookDaoImpl> bookDaoList) {
this.bookDaoList = bookDaoList;
}
@Override
public void save() {
System.out.println(bookDaoList);
System.out.println("book service save ...");
}
}
bean创建代码:
因为我们这里是复杂类型的集合,所以我们要设置我们的dao这个bean为
多个bean创建
注意:property的name要跟setter方法名字setBookDaoimpl后面的后缀一样,否则会报错,ref指bean的id
<bean id="bookDao" scope="prototype" class="com.itheima.dao.impl.BookDaoImpl"/>
<bean id="bookService" class="com.itheima.service.impl.BookServiceImpl">
<property name="bookServiceImpl">
<list>
<ref bean="bookDao"/>
<ref bean="bookDao"/>
</list>
</property>
</bean>
结果:
注意:
如果我们要注入第三方的类,无论是通过setter方法进行注入还是通过构造方法来进行注入,我们都要阅读该类看该类是实现了构造方法还是setter方法
我们找到我们的类,同时按住Ctrl和F12(如果有Fn,也需要按住)
我们发现druid的类构造方法里面的形参,无法进行依赖注入
然后我们输入setusername发现里面,在里面有一个形参类型为string的类型,这里依赖注入的是数据库用户名,说明,如果我们要依赖注入就要使用settter方法进行依赖注入
例如:
我们使用的类型是第三方复杂类型(druid,阿里巴巴的数据库连接池)
配置文件代码:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.1.12</version>
</dependency>
<!--druid的作用是数据库连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.20</version>
</dependency>
</dependencies>
第一种依赖注入第三方bean方法(直接依赖注入)
目录:
属性bean代码:
driverClassName:数据库连接池驱动类
url:数据库的地址
username:用户名
password:密码
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value=""/>
<property name="url" value=""/>
<property name="username" value=""/>
<property name="password" value=""/>
</bean>
测试代码:
package com.itheima;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import javax.sql.DataSource;
public class Main {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
DataSource dataSource = (DataSource) applicationContext.getBean("dataSource");
System.out.println(dataSource);
}
}
结果:
第二种依赖注入第三方bean方法:(读取配置文件)
目录:
jdbc.properties代码:
driverClassName:数据库连接池驱动类
url:数据库的地址
username:用户名
password:密码
jdbc.driver=
jdbc.url=
jdbc.username=
jdbc.password=
首先我们需要开启context命名空间,以便下面的读取操作
注意:化红线的是我们需要添加的代码,并且注意双引号的位置
相关代码:
<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的命名空间
<context:property-placeholder location="classpath*:jdbc.properties,jdbc.properties"/>
bean创建代码:
我们通过${}占位符来读取properties文件的属性,进行依赖注入
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<!--配置数据库连接信息-->
<!--数据库驱动类-->
<property name="driverClassName" value="${jdbc.driver}"/>
<!--数据库地址-->
<property name="url" value="${jdbc.url}"/>
<!--数据库用户名-->
<property name="username" value="${jdbc.username}"/>
<!--数据库密码-->
<property name="password" value="${jdbc.password}"/>
</bean>
测试代码:
package com.itheima;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import javax.sql.DataSource;
public class Main {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
DataSource dataSource = (DataSource) applicationContext.getBean("dataSource");
System.out.println(dataSource);
}
}
结果:
注意:
我们的jdbc.properties的书写也有规范
例如:
配置文件代码:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.1.12</version>
</dependency>
</dependencies>
目录:
context创建方法与上述相同这里就不再演示了
实现dao类代码:
package com.itheima.dao.impl;
import com.itheima.dao.BookDao;
public class BookDaoImpl implements BookDao {
private String mysql_pool_name;
@Override
public void save()
{
System.out.println("book dao save ..."+"name: "+this.mysql_pool_name);
}
public void setMysqlname(String mysqlname) {
this.mysql_pool_name = mysqlname;
}
}
jdbc.properties代码:
username=root666
bean创建代码:
<bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl">
<property name="mysqlname" value="${username}"/>
</bean>
测试代码:
package com.itheima;
import com.itheima.dao.impl.BookDaoImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
BookDaoImpl bookDao = (BookDaoImpl) applicationContext.getBean("bookDao");
bookDao.save();
}
}
结果:
我们会发现我们的mysql_pool_name并不是从配置文件当中读取的,这是因为我们计算机系统属性比配置文件读取的优先级高,并且我们的username与计算机里面的username相同,所以才不是我们想要的结果,解决办法就是在开启context的命名空间加上代码,表示不读取计算机的属性
代码:
<context:property-placeholder location="classpath*:jdbc.properties,jdbc2.properties" system-properties-mode="NEVER"/>
修改后的结果:
自动注入:
目录:
实现dao类代码:
package com.itheima.dao.impl;
import com.itheima.dao.BookDao;
public class BookDaoImpl implements BookDao {
@Override
public void save()
{
System.out.println("book dao save ...");
}
}
实现service类代码:
package com.itheima.service.impl;
import com.itheima.dao.impl.BookDaoImpl;
import com.itheima.service.BookService;
public class BookServiceImpl implements BookService {
private BookDaoImpl bookDaoimpl;
@Override
public void save() {
System.out.println("book service save ...");
bookDaoimpl.save();
}
public void setBookDao(BookDaoImpl bookDao) {
this.bookDaoimpl = bookDao;
}
}
bean创建代码:
autowire的bytype,bean类型必须唯一,并且与set方法的形参类型一致
byname,bean名称必须唯一,并且和set方法名一致,但是耦合度高
<bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl"/>
<bean id="bookService" class="com.itheima.service.impl.BookServiceImpl" autowire="bytype" />
结果: