1.导入Spring开发所用的包(如果使用Maven配置坐标即可)
2.编写Dao接口和实现类
3.根据实现类的内容创建Spring配置文件(.XML文件)
4.根据对UserDao对象的要求配置.xml文件
5.获取Spring所产生的Bean实例
说明:
FileSystemXmlApplicationContext:路径为盘符路径
ClassPathXmlApplicationContext:路径为文件路径
第一步:创建一个类,设置属性以及对于的set方法
public class Book {
public String auto;
public String name;
public void setAuto(String auto) {
this.auto = auto;
}
public void setName(String name) {
this.name = name;
}
}
第二步:在spring配置文件(.xml文件)中配置对象的创建以及属性的注入
<bean id="book" class="Test_Spring5.Book">
<property name="auto" value="123">property>
<property name="name" value="456">property>
bean>
第三步:使用
代码:
public class Main {
public static void main(String[] args) {
//1.加载spring配置文件
//FileSystemXmlApplicationContext:路径为盘符路径
//ClassPathXmlApplicationContext:路径为文件路径
ApplicationContext context=
new ClassPathXmlApplicationContext("Test_spring5.xml");
//2.创建对象
/*
getBean:Spring IOC容器中获取bean实例
格式:getBean(String name,Class requiredType)
name:要获取的javaBean的标识
requiredType:要获取的javaBean的类型
*/
Book book=context.getBean("book",Book.class);
System.out.println(book.auto);
}
}
这种方法底层还是使用set,但是更简化
第一步:在xml文件中的beans中添加xmlns:p="http://www.springframework.org/schema/p"
第二步:配置bean
<bean id="book" class="Test_Spring5.Book"
p:auto="好好学习"
p:name="天天向上"
>bean>
第三步:使用
public class Main {
public static void main(String[] args) {
ApplicationContext context=
new ClassPathXmlApplicationContext("Test_spring5.xml");
Book fin=context.getBean("book",Book.class);
System.out.println(fin.auto+fin.name);
}
}
第一步:创建一个类,设置属性以及构造器
public class finance {
public String date;
public String name;
public finance(String date, String name) {
this.date = date;
this.name = name;
}
}
第二步:在xml文件里面配置
<bean id="finance" class="Test_Spring5.finance">
<constructor-arg name="date" value="2021">constructor-arg>
<constructor-arg name="name" value="郝佳顺">constructor-arg>
bean>
或者
<bean id="finance" class="Test_Spring5.finance">
<constructor-arg index="0" value="2021">constructor-arg>
<constructor-arg index="1" value="郝佳顺">constructor-arg>
bean>
第三步:使用
public class Main {
public static void main(String[] args) {
ApplicationContext context=
new ClassPathXmlApplicationContext("Test_spring5.xml");
finance fin=context.getBean("finance",finance.class);
System.out.println(fin.date+fin.name);
}
}
<bean id="book" class="Test_Spring5.Book">
<property name="name">
<null/>
property>
bean>
<bean id="book" class="Test_Spring5.Book">
<property name="name">
<value>>]]>value>
property>
bean>
第一步:创建两个不同的实现类
我们要做的是在Service中调用Dao里面的方法
也就是
在Service中注入Dao
第二步:在Service中创建Dao中属性并生成set方法
public class userServiceInter implements userService{
public userDao demo;
public void setDemo(userDao demo){
this.demo=demo;
}
public void add() {
System.out.println("user......");
demo.add();//调用
}
}
第三步:在.xml文件中配置
<bean id="userService" class="Service.userServiceInter">
<property name="demo" ref="userDao">property>
bean>
<bean id="userDao" class="Dao.userDaoInter">bean>
第四步:编写测试代码
public class Main {
public static void main(String[] args) {
//加载配置文件
ApplicationContext context=
new ClassPathXmlApplicationContext("bean.xml");
//创建service的对象
userService user=context.getBean("userService",userService.class);
//输出
user.add();
}
}
举例部门与员工之间的一对多关系
第一步:创建员工类和部门类,生成对应的set方法
员工类:
public class Employee {
String name;
String age;
//设置唯一的部门对象
Department department;
public void setDepartment(Department department) {
this.department = department;
}
public void setName(String name) {
this.name = name;
}
public void setAge(String age) {
this.age = age;
}
public void add(){
System.out.println(name+" "+age);
department.add();
}
}
部门类
public class Department {
String name;
String number;
public void setName(String name) {
this.name = name;
}
public void setNumber(String number) {
this.number = number;
}
public void add(){
System.out.println(name+" "+number);
}
}
第二步:配置.xml文件
<bean id="employee" class="Employee.Employee">
<property name="name" value="员工1">property>
<property name="age" value="30">property>
<property name="department">
<bean id="department" class="Department.Department">
<property name="name" value="科技公司">property>
<property name="number" value="139XXXXXX22">property>
bean>
property>
bean>
以下两种方法是在内部Bean注入
的基础上改写
第一种:
<bean id="employee" class="Employee.Employee">
<property name="name" value="员工1">property>
<property name="age" value="30">property>
<property name="department" ref="department">property>
bean>
<bean id="department" class="Department.Department">
<property name="name" value="科技公司">property>
<property name="number" value="150XXXX2631">property>
bean>
第二种:
第一步:在员工类中添加get公司对象的方法
public class Employee {
String name;
String age;
Department department;
//get公司对象
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
public void setName(String name) {
this.name = name;
}
public void setAge(String age) {
this.age = age;
}
public void add(){
System.out.println(name+" "+age);
department.add();
}
}
第二步:修改.xml文件
<bean id="employee" class="Employee.Employee">
<property name="name" value="员工1">property>
<property name="age" value="30">property>
<property name="department" ref="department">property>
<property name="department.name" value="网络公司" >property>
<property name="department.number" value="10001" >property>
bean>
<bean id="department" class="Department.Department">
<property name="name" value="科技公司">property>
<property name="number" value="150XXXX2631">property>
bean>
public class exhi {
public String srt[];
public Map<String,String> map;
public List<String> list;
public void setSrt(String[] srt) {
this.srt = srt;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public void setList(List<String> list) {
this.list = list;
}
public void add(){
System.out.println(Arrays.toString(srt));
System.out.println(list);
System.out.println(map);
}
}
<bean id="exhibition" class="exhibition.exhi">
<property name="srt">
<array>
<value>好好学习!value>
<value>天天向上!value>
<value>成长万岁!value>
array>
property>
<property name="list">
<list>
<value>好好想学习1value>
<value>天天向上1value>
<value>成长万岁1value>
list>
property>
<property name="map">
<map>
<entry key="好好学习" value="天天向上">entry>
map>
property>
bean>
1.在类中创建一个List集合,限定属性为对象
public class student {
String name;
String age;
//在类中创建一个List集合,限定属性为对象:
List<course> list;
public void setName(String name) {
this.name = name;
}
public void setAge(String age) {
this.age = age;
}
public void setList(List<course> list) {
this.list = list;
}
}
2.配置文件
<bean id="student1" class="Student.student">
<property name="name" value="小明">property>
<property name="age" value="17">property>
<property name="list">
<list>
<ref bean="course1">ref>
<ref bean="course2">ref>
list>
property>
bean>
<bean id="course1" class="Course.course">
<property name="name" value="高数">property>
bean>
<bean id="course2" class="Course.course">
<property name="name" value="计组">property>
bean>
在xml文件中设置一个公共的list集合
第一步:在xm头文件中添加和修改下面代码
源代码:
<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">
修改:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
添加1:
xmlns:util="http://www.springframework.org/schema/util"
添加2:复制下面代码并追加,将beans都修改为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">
第二步:创建公用的list集合
<util:list id="publiclist">
<value>好好学习value>
<value>天天向上value>
util:list>
第三步:使用
<bean id="student" class="Student.student">
<property name="list" ref="publiclist">property>
bean>
完整代码:
<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">
<util:list id="publiclist">
<value>好好学习value>
<value>天天向上value>
util:list>
<bean id="student" class="Student.student">
<property name="list" ref="publiclist">property>
bean>
beans>
普通bean:配置文件中限制了Bean的类型
工厂bean:返回的Bean的类型可以不是配置文件中的类型;
操作:
第一步:配置xml文件原始类的Bean
<bean id="cous" class="Couse.Cous"> bean>
第二步:使原始类实现`FactoryBean``接口、限制泛型、重写方法
public class Cous implements FactoryBean<Teacher> {
//定义返回的Bean
@Override
public Teacher getObject() throws Exception {
Teacher tea=new Teacher();
tea.setName("haohaohao");
return tea;
}
@Override
public Class<?> getObjectType() {
return null;
}
@Override
public boolean isSingleton() {
return FactoryBean.super.isSingleton();
}
}
第三步:使用
ApplicationContext context=new ClassPathXmlApplicationContext("bean.xml");
//获取的Bean的名称还是"cous",但类型需要改变为Teacher.class
Teacher ter=context.getBean("cous",Teacher.class);
System.out.println(ter.name);
Bean默认是单实例对象,如果想修改为多实例,可以修改xml配置文件实现
<bean id="cous" class="Couse.Cous" scope="prototype"> bean>
scope="prototype" :多实例,调用getBean()方法时候创建对象
scope="singleton" :单实例(默认),加载配置文件时候创建对象
public class Demo1 {
public String name;
public Demo1(){
System.out.println("第一步,无参构造器创建Bean");
}
public void setName(String name) {
this.name = name;
System.out.println("第二步,调用Set方法设置属性");
}
public void initMethod(){
System.out.println("第三步,调用初始化方法");
}
public void destMethod(){
System.out.println("第五步,调用销毁方法");
}
}
public class Main {
public static void main(String[] args) {
//ApplicationContext 里面没有close方法,选择其子实现类
ClassPathXmlApplicationContext context=
new ClassPathXmlApplicationContext("bean1.xml");
Demo1 demo1=context.getBean("Demo1",Demo1.class);
System.out.println("第四步:获取对象");
System.out.println(demo1);
//第五步 销毁
context.close();
}
}
init-method:设置初始化方法
destroy-method:设置销毁方法
<bean id="Demo1" class="Demo1.Demo1" init-method="initMethod" destroy-method="destMethod">
<property name="name" value="999999">property>
bean>
1.后置处理调用在获取对象的前后,目的是对配置文件中所有的bean对象进行设置
2.继承BeanPostProcessor的类便为后置处理器的类
配置后置处理器
//继承BeanPostProcessor便为后置处理器的类
public class MebeanPost implements BeanPostProcessor {
//@Override简单理解就是这个句话下边的方法是继承父类的方法,对其覆盖
//Bean初始化前调用的方法
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("Bean要初始化了");
return bean;
}
//Bean初始化后调用的方法
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("bean初始化完了");
return bean;
}
}
<bean id="Demo1" class="Demo1.Demo1" init-method="initMethod" destroy-method="destMethod">
<property name="name" value="999999">property>
bean>
<bean id="MebeanPost" class="Demo1.MebeanPost">bean>
beans>
//根据名称寻找对应的注入bean
<bean id="" class="" autowire="byName">bean>
//确保类bean唯一,根据类型寻找对应的注入bean
<bean id="" class="" autowire="byType">bean>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver">property>
<property name="url" value="jdbc:mysql://localhost:3306/数据库名称">property>
<property name="username" value="root">property>
<property name="password" value="root">property>
bean>
1.创建properties文件
以键值对的方式写入,等号前面名称随便起
<!--数据库驱动名称-->
prop.driverClass=com.mysql.jdbc.Driver
<!--数据库地址-->
prop.url=jdbc:mysql://localhost:3306/数据库名称
<!--链接数据库的用户名和密码-->
prop.userName=root
prop,password=root
2.引入properties文件到Spring中
引入名称空间
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
3.xml文件引入
//引入外部文件
<context:property-placeholder location="classpath:jdbc.properties"/>
//${}的方式提取
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${prop.driverClass}">property>
<property name="url" value="${prop.url}">property>
<property name="username" value="${prop.userName}">property>
<property name="password" value="${prop,password}">property>
bean>
beans>
1. getBean:Spring IOC容器中获取bean实例
格式:getBean(String name,Class requiredType)
name:要获取的javaBean的标识
requiredType:要获取的javaBean的类型
2. FileSystemXmlApplicationContext:路径为盘符路径
ClassPathXmlApplicationContext:路径为文件路径
3. property表示注入属性(set注入)
name表示要注入的属性名 vlaue表示要注入的属性值
<property name="auto" value="123">property>
<property name="name" value="456">property>
4. constructor-arg表示构造器注入
<constructor-arg name="date" value="2021">constructor-arg>
<constructor-arg name="name" value="郝佳顺">constructor-arg>
5. 级联赋值:ref与要赋予的bean的id同名
<bean id="employee" class="Employee.Employee">
<property name="department" ref="department">property>
bean>
<bean id="department" class="Department.Department">
<property name="name" value="科技公司">property>
<property name="number" value="150XXXX2631">property>
bean>
6.遍历数组:Arrays.toString(srt);
7.集合中注入对象
<property name="list">
<list>
<ref bean="course1">ref>
<ref bean="course2">ref>
list>
property>