<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”>
4.0.0
org.example
spring_day01_spring1
1.0-SNAPSHOT
jar
org.springframework
spring-context
5.0.2.RELEASE
创建业务层接口和实现类:
/**
-
@Author: Ly
-
@Date: 2020-07-21 16:29
-
账户业务层的接口
*/
public interface IAccountService {
/**
- 模拟保存账户
*/
void saveAccount();
}
/**
-
@Author: Ly
-
@Date: 2020-07-21 16:31
-
账户的业务成实现类
*/
public class AccountServiceImpl implements IAccountService {
private IAccountDao accountDao=new AccountDaoImpl();
public AccountServiceImpl(){
System.out.println(“对象创建了”);
}
public void saveAccount() {
accountDao.saveAccount();
}
}
创建持久层接口和实现类:
/**
-
@Author: Ly
-
@Date: 2020-07-21 16:33
-
账户的持久层接口
*/
public interface IAccountDao {
/**
- 模拟保护账户
*/
void saveAccount();
}
/**
- 账户的持久层实现类
*/
public class AccountDaoImpl implements IAccountDao {
public void saveAccount() {
System.out.println(“保存了账户”);
}
}
配置Bean.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”
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
测试代码:
public class Client {
/*
-
获取spring的Ioc核心容器,并根据id获取对象
-
ApplicationContext的三个常用实现类
-
ClassPathXmlApplicationContext: 它可以加载类路径下的配置文件,要求配置文件必须在类路径下。不在的话,加载不了
-
FileSystemXmlApplicationContext: 他可以加载磁盘任意路径下的配置文件(必须有访问权限)
-
AnnotationConfigApplicationContext:它是用于读取注解创建容器的
-
核心容器的两个接口引发出的问题:
-
ApplicationContext: 单例对象适用 一般采用此接口
-
它在创建核心容器时,创建对象采用的策略是采用立即加载方式,只要一读取完配置文件马上就创建文件中的配置对象。
-
BeanFactory: 多例对象适用
-
它在创建核心容器时,创建的对象采取的策略是采用延迟加载的方式,什么时候id获取对象了,什么时候才真正的创建对象。
*/
public static void main(String[]args){
//1.获取核心容器对象
ApplicationContext ac=new ClassPathXmlApplicationContext(“bean.xml”);
//ApplicationContext ac=new FileSystemXmlApplicationContext(“F:\Intellij idea\spring\spring_day01_spring1\src\main\resources\bean.xml”);//配置文件对应的磁盘中的绝对路
//2.根据id获取Bean对象
IAccountService as=(IAccountService)ac.getBean(“accountService”);
IAccountDao ad=ac.getBean(“accountDao”,IAccountDao.class);
/*//------BeanFactory获取核心容器对象---------
Resource resource=new ClassPathResource(“bean.xml”);
BeanFactory factory=new XmlBeanFactory(resource);
IAccountService as=(IAccountService)factory.getBean(“accountService”);
IAccountDao ad=factory.getBean(“accountDao”,IAccountDao.class);*/
System.out.println(as);
System.out.println(ad);
}
}
运行结果:
从结果中可以看出,我们并没有使用new的方式实例化对象,但是我们的对象已经被实例化了。
Bean是Spring容器初始化、装配和管理的对象,它是由 spring 来创建的,默认情况下它调用的是类中的无参构造函数。如果没有无参构造函数则不能创建成功。
标签的属性:<bean>
id: 给对象在容器中提供一个唯一标识。用于获取对象。
class: 指定类的全限定类名。用于反射创建对象。默认情况下调用无参构造函数。
scope: 指定对象的作用范围。
-
singleton :默认值,单例的.
-
prototype :多例的.
-
request :WEB 项目中,Spring 创建一个 Bean 的对象,将对象存入到 request 域中.
-
session :WEB 项目中,Spring 创建一个 Bean 的对象,将对象存入到 session 域中.
-
global session :WEB 项目中,应用在 Portlet 环境.如果没有 Portlet 环境那么globalSession 相当于 session
init-method: 指定类中的初始化方法名称。
destroy-method: 指定类中销毁方法名称。
创建Bean的三种方式
项目结构:
第一种方式:根据默认无参构造函数来创建类对象。如果 bean 中没有默认无参构造函数,将会创建失败。在spring的配置文件中使用bean标签,配以id和class属性之后,且没有其它属性和标签时,采用的就是默认函数创建bean对象,此时如果类中没有默认构造函数则对象无法创建。
第二种方式:使用普通工厂中的方法创建对象(使用某个类中的方法创建对象,并存入spring容器)
/**
-
@Author: Ly
-
@Date: 2020-07-23 23:27
-
模拟一个工厂类(该类可能是存在于jar包中的,我们无法通过修改源码的方式来提供默认构造函数)
*/
public class InstanceFactory {
public IAccountService getAccountService(){
return new AccountServiceImpl();
}
}
第三种方法:使用工厂中的静态方法创建对象(使用某个静态方法创建对象,并存入spring容器)
/**
-
@Author: Ly
-
@Date: 2020-07-23 23:27
-
模拟一个工厂类(该类可能是存在于jar包中的,我们无法通过修改源码的方式来提供默认构造函数)
*/
public class StaticFactory {
public static IAccountService getAccountService(){
return new AccountServiceImpl();
}
}
bean 的作用范围和生命周期:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns=“http://www.springframework.org/schema/beans”
xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id=“accountService” class=“com.ly.service.impl.AccountServiceImpl”
scope=“prototype” init-method=“init” destroy-method=“destroy”>
依赖注入:Dependency Injection, 它是 spring 框架核心 IOC的具体实现。
我们的程序在编写时,通过控制反转,把对象的创建交给了 spring,但是代码中不可能出现没有依赖的情况。我么知道IOC的作用是降低程序间的耦合,而不是消除耦合。对于我们项目中业务层和持久层的依赖关系,我们可以交给 spring 来维护,在我们的类中需要用到其他类的对象时,可以由由spring为我们提供。通过spring框架把持久层对象传入到业务层,从而省去我们自己获取的步骤。
依赖注入可以注入三种类型的数据:基本类型和string、其他bean类型(在配置文件中或者注解配置过的bean)、复杂类型/集合类型
同时有三种注入方式:构造函数提供、set方式提供、注解提供
案例:
构造函数注入:
通过使用类中的构造函数,给成员变量赋值。在这之中,赋值的操作不是我们自己做的,而是通过配置的方式,让 spring 框架来为我们注入。具体代码如下:
/**
-
@Author: Ly
-
@Date: 2020-07-21 16:31
-
账户的业务成实现类
*/
public class AccountServiceImpl implements IAccountService {
//如果是经常变化的数据,并不适用于注入的方式
private String name;
private Integer age;
private Date birthday;
public AccountServiceImpl(String name,Integer age,Date birthday){
this.name=name;
this.age=age;
this.birthday=birthday;
}
public void saveAccount() {
System.out.println(“service中的saveAccount方法执行了,”+name+“,”+age+“,”+birthday);
}
}
set方法注入
在类中提供需要注入成员的 set 方法,进行注入。具体代码如下:
/**
-
@Author: Ly
-
@Date: 2020-07-21 16:31
-
账户的业务成实现类
*/
public class AccountServiceImpl2 implements IAccountService {
//如果是经常变化的数据,并不适用于注入的方式
private String name;
private Integer age;
private Date birthday;
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public void saveAccount() {
System.out.println(“service中的saveAccount方法执行了,”+name+“,”+age+“,”+birthday);
}
}
复杂类型注入:
依赖注入可以注入复杂的数据类型,如s数组、集合,同时注入这些复杂数据也是set方法注入的方式,只不过变量的数据类型都是集合。具体代码如下:
/**
-
@Author: Ly
-
@Date: 2020-07-21 16:31
-
账户的业务成实现类
*/
public class AccountServiceImpl3 implements IAccountService {
private String[] myStrs;
private List myList;
private Set mySets;
private Map<String,String> myMap;
private Properties myProps;
public void setMyStrs(String[] myStrs) {
this.myStrs = myStrs;
}
public void setMyList(List myList) {
this.myList = myList;
}
public void setMySets(Set mySets) {
this.mySets = mySets;
}
public void setMyMap(Map<String, String> myMap) {
this.myMap = myMap;
}
public void setMyProps(Properties myProps) {
this.myProps = myProps;
}
public void saveAccount() {
System.out.println(Arrays.toString(myStrs));
System.out.println(myList);
System.out.println(mySets);
System.out.println(myMap);
System.out.println(myProps);
}
}
AAA
BBB
CCC
AAA
BBB
CCC
最后
对于很多Java工程师而言,想要提升技能,往往是自己摸索成长,不成体系的学习效果低效漫长且无助。
整理的这些资料希望对Java开发的朋友们有所参考以及少走弯路,本文的重点是你有没有收获与成长,其余的都不重要,希望读者们能谨记这一点。
再分享一波我的Java面试真题+视频学习详解+技能进阶书籍
t);
System.out.println(mySets);
System.out.println(myMap);
System.out.println(myProps);
}
}
AAA
BBB
CCC
AAA
BBB
CCC
最后
对于很多Java工程师而言,想要提升技能,往往是自己摸索成长,不成体系的学习效果低效漫长且无助。
整理的这些资料希望对Java开发的朋友们有所参考以及少走弯路,本文的重点是你有没有收获与成长,其余的都不重要,希望读者们能谨记这一点。
再分享一波我的Java面试真题+视频学习详解+技能进阶书籍
[外链图片转存中…(img-X88yFMSF-1714458001196)]