Spring框架的学习—day03
Spring中的ref引用
Spring中的ref引用:因为在使用java编程时,在一个类中不一定只用自己的属性,还可能引用其他的类
当在一个类中引用了另一个类的时候,通过ref指定引用的是哪个类。
public class User {
private int id;
private String name;
private String address;
private Car car;//引用的类
}
public class Car {
private int id;
private String name;
private String color;
private String address;
}
<bean id="user" class="com.zll.spring.User">
<property name="id" value="123"></property>
<property name="name" value="zll"></property>
<property name="address" value="666"></property>
<property name="car" ref="car"></property>
</bean>//ref中的car和id中的car一样
<bean id="car" class="com.zll.spring.Car">
<property name="id" value="8888"></property>
<property name="name" value="奔驰"></property>
<property name="color" value="黑色"></property>
<property name="address" value="西安"></property>
</bean>
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("com\\zll\\spring\\bean.xml");
System.out.println(ac);
User user=ac.getBean("user",User.class);
System.out.println(user.getId()+"\t"+user.getName()+"\t"
+user.getAddress()+"\t"+user.getCar().getId());
}
使用构造器注入时也一样
Spring框架的工作原理
1.main程序通过applicationContext来加载xml文件,当对象被创建的时候,spring的容器也就启动了,它通过加载xml文件,找到xml
2.通过xml文件解析(Spring解析xml文件时是通过dom4j来解析的,通过解析找到class)的方式把xml文件的内容(属性以及它的引用)拿到。
3.通过class的名字找到类(使用反射机制),把属性注入到类的对象上,bean对象就产生了,在内存空间中就有了栈内存和堆内存中了。栈内存存储对象的引用,堆内存存储对象的内容,这个对象是通过工厂模式创建,调用beanfactory来创建对象。
4.当使用applicationxontext调用bean的时候,它就可以被使用了。
我们在使用spring的时候,每次都要创建一个applicationcontext ,可以把他进行抽象, 生成一个类然后提供一个方法, 以后使用的时候直接调用方法即可。这样的话以后直接调用这个类的方法就可以到我们需要的spring容器。
public class ApplicationContextUtils {
public static ApplicationContext getApplicationContext(String path){
return new ClassPathXmlApplicationContext(path);
}
}
Spring的注入方式
1.xml文件注入(装配):进行操作的时候用的是set注入或构造器注入。
2.接口注入,用的非常少
3.注解注入:最常见的方式
Spring中提供了4个注解,这些注解和xml文件上作用是一样的,也是注入到我们的容器中,默认的时候都会使用类的名字来进行。
@component:当前组件的层次关系难以定位的时候
@controller:表示它是控制层
@service:表示业务逻辑层
@respostitory:表示的是数据访问层
当你在使用spring的注解的时候,要在xml文件中去扫描注解的包。
1.先在实体类上通过注解方式来写内容,以及注入的具体的内容。
@Component("teacher")
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Teacher {
@Value("zll")
private String name;
@Value("西安")
private String address;
}
@Component("student")
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Student {
@Value("lxy")
private String name;
@Value("北京")
private String address;
@Resource
private Teacher teacher;
}
2.在xml文件中写要扫描的实体类所在的位置。
项目启动的时候指定这个xml文件;,这个文件被读取的时候他就取扫描这个包, 在通过类上的注解注入我们的属性。
使用注解的时候他会自动扫描包, 通过这个包中的类就能创建对象。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="zom.zll.spring02"></context:component-scan>
</beans>
直接使用类的方式注入
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private int id;
private String name;
}
@Data()
@AllArgsConstructor
@NoArgsConstructor
public class Teacher {
private int id;
private String name;
}
创建一个config 类: 这个类主要的作用就是返回对象。一定要和注解连用:@Configuration 这个就代表是配置类;然后在具体方法上写@Bean注解,代表方法返回具体使用的bean
@Configuration
public class Config {
@Bean
public Student getStudent(){
Student student=new Student();
student.setId(12);
student.setName("zll");
return student;
}
@Bean
public Teacher getTeacher(){
Teacher teacher=new Teacher();
teacher.setId(22);
teacher.setName("lxy");
return teacher;
}
}
测试时候使用的注解AnnotationConfigApplicationContext 使用注解方式返回一个applicationContext, 然后就可以调用。这个就是完全不使用xml文件的方式来进行注入。
public class Test {
public static void main(String[] args) {
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(Config.class);
System.out.println(ac.getBean(Teacher.class).getName());
System.out.println(ac.getBean(Student.class).getName());
}
}
Spring注入数组,对象数组, list,set, map,属性文件
Spring注入数组
,
<bean id="student" class="com.zll.spring04.Student">
<property name="id" value="1"></property>
<property name="name" value="zll"></property>
<property name="address" value="西安"></property>
</bean>
<bean id="student1" class="com.zll.spring04.Student">
<property name="id" value="12"></property>
<property name="name" value="zll1"></property>
<property name="address" value="西安1"></property>
</bean>
<bean id="student2" class="com.zll.spring04.Student">
<property name="id" value="13"></property>
<property name="name" value="zll2"></property>
<property name="address" value="西安2"></property>
</bean>
<bean id="teacher" class="com.zll.spring04.Teacher">
<property name="tid" value="2"></property>
<property name="tname" value="lxy"></property>
<property name="taddress" value="西安"></property>
<property name="number">
<array> //基本类型
<value>12</value>
<value>13</value>
<value>14</value>
<value>15</value>
<value>16</value>
</array>
</property>
<property name="students">
<list> //引用
<ref bean="student"></ref>
<ref bean="student1"></ref>
<ref bean="student2"></ref>
</list>
</property>
</bean>
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Student {
private int id;
private String name;
private String address;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Teacher {
private int tid;
private String tname;
private String taddress;
private int []number;
private Student[] students;
}
public class Test {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("com/zll/spring04/bean.xml");
Student student=ac.getBean("student",Student.class);
System.out.println(student);
Teacher teacher=ac.getBean("teacher",Teacher.class);
System.out.println(teacher.getTid()+"\t"+teacher.getTname()+"\t"+teacher.getTaddress());
int []number=teacher.getNumber();
for (int i : number) {
System.out.println(i);
}
Student []students=teacher.getStudents();
for (Student student1 : students) {
System.out.println(student1.getId()+"\t"+student1.getName()+"\t"+student1.getAddress());
}
}
}
spring注入list
<property name="list">
<list>
<value>中国</value>
<value>美国</value>
<value>英国</value>
<ref bean="student"></ref>
<ref bean="student1"></ref>
</list>
</property>
List list=teacher.getList();
for (Object o : list) {
System.out.println(o);
}
spring注入set
<property name="school">
<set>
<value>西北大学</value>
<value>交大</value>
<value>北大</value>
<ref bean="student1"></ref>
<ref bean="student2"></ref>
</set>
</property>
spring注入map
<property name="map">
<map>
<entry key="第一次" value="111"></entry>
<entry key="第二次" value="222"></entry>
<entry key="第三次" value="333"></entry>
<entry key="第五次" value-ref="student"></entry>
<entry key-ref="student1" value="666"></entry>
</map>
</property>
Map<Object,Object> map=teacher.getMap();
for(Map.Entry<Object,Object> entry:map.entrySet()){
Object entrykey=entry.getKey();
Object entryvalue=entry.getValue();
System.out.println(entrykey+":"+entryvalue);
}
Spring注入properties(属性文件的内容)
<property name="properties">
<props>
<prop key="drive">com.mysql.jdbc.Driver</prop>
<prop key="url">jdbc:mysql://localhost:3306/test</prop>
<prop key="user">root</prop>
<prop key="password">1234</prop>
</props>
</property>
Properties properties=teacher.getProperties();
for (Object o : properties.entrySet()) {
System.out.println(o);
}