bean管理指的是两件事:
1.spring创建对象
2.属性注入 给对象中的属性赋值
1.bean创建对象:
无参构造函数
xml文件:
<bean id ="user" class="com.atuguigu.spring5.User"></bean>
属性值:id是唯一的标识 ,也别的也可以 ,class是对象所在的全路径 包名.类名
创建对象的时候,默认执行的是无参的构造函数,所以需要类中要有无参构造函数,否则会报错
有参构造函数
java如果定义了一个有参构造函数,则不会自动生成无参的构造函数,和c++一样,这时如果不想重新写无参构造函数,就想使用有参的构造函数,应该这样写
<bean id="user1" class="com.testdemo.User">
<!-- index代表 第几个属性 -->
<constructor-arg index="0" value="10"/>
<constructor-arg index="1" value="name1"/>
</bean>
test类中生成user对象
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("demo1.xml");
//第一个参数是xml文件中该类对应的bean的id的值
User user =(User) applicationContext.getBean("user1",User.class);
System.out.println(user.toString());
}
}
有三种属性注入的方法:
1)set方法
1……在user类中创建属性,并生成对应的set方法
public class User {
public User(int aa) {
this.aa = aa;
}
public User() {
}
public void setAa(int aa) {
this.aa = aa;
}
public int aa;
public static void add1()
{
System.out.println("nihao shijie ");
}
}
2…… 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">
<!--配置user对象创建-->
<bean id ="user" class="com.atuguigu.spring5.User">
<property name="aa" value="aavalue"></property>
</bean>
</beans>
2)简化的set p名称空间注入
实际用的不多
1…在xml文件中进行属性注入
<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”
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/
2…在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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean name="student-config" class="com.atuguigu.spring5.testdemo.Student" p:id="20" p:loc="loc2" p:name="name2">
</bean>
</beans>
3)通过xml文件设置属性的空值和特殊值
<![CDATA[<北京>]]>
<北京> 是要放的特殊值 其他的是固定格式 也这样写就行了
4)通过xml方式注入集合类型属性
1.注入数组类型属性
2.注入list 集合类型
3.注入map 集合类型
4.注入 set集合类型
5.注入 list<自定义类> 集合类型
<?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="stu" class="com.testdemo.Student">
<property name="courses">
<array>
<value>"java_array"</value>
<value>"python_array"</value>
<value>"c++_array"</value>
</array>
</property>
<property name="list">
<list>
<value>"java_list"</value>
<value>"python_list"</value>
<value>"c++_list"</value>
</list>
</property>
<property name="map">
<map>
<entry key="JAVA_map" value="java_map"></entry>
<entry key="PYTHON_map" value="python_map"></entry>
<entry key="c++_map" value="c++_map"></entry>
</map>
</property>
<property name="set">
<set>
<value>"set java"</value>
<value>"set python"</value>
<value>"set c++"</value>
</set>
</property>
<property name="lessons" >
<list>
<ref bean="lesson1"></ref>
<ref bean="lesson2"></ref>
</list>
</property>
</bean>
<bean id="lesson1" class="com.testdemo.Lession">
<property name="name" value="java"></property>
</bean>
<bean id="lesson2" class="com.testdemo.Lession">
<property name="name" value="python"></property>
</bean>
</beans>
外部bean
一个service类中要调用dao类,可以把dao类看做是service类的一个属性,当做属性来 声明 和 注入
public class Studentservice {
Studentdao studentdao =new Studentdaoimpl();
//set方法是为了在配置文件中生成service类时可以给这个属性赋值
public void setBooks(Books books2) {
this.books=books2;
}
return studentdao;
}
外部类指的是我在service类声明的外面 , 声明dao 类 ,然后在service类中引用dao类
<?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="dao" class="com.testdemo.dao.Studentdaoimpl"></bean>
<bean id="service" class="com.testdemo.service.Studentservice">
<property name="studentdao" ref="dao" ></property>
<!-- 将上面的dao对象传给service中的属性-->
<!--因为参数是一个类,所以要用ref-->
</bean>
</beans>
public static void main(String[] args) {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("demo1.xml");
//第一个参数是xml文件中该类对应的bean 的 id值
Studentservice studentservice =(Studentservice) applicationContext.getBean("service" ,Studentservice.class);
studentservice.print2();
}
内部bean
在外部类里面声明内部的类 ,(可能没说清楚,看下面代码),
student里面声明department类, department里面有个属性 d_id,也给这个属性赋值
<bean id="student" class="com.testdemo2.Student">
<property name="department">
<bean id="department2" class="com.testdemo2.Department">
<property name="d_id" value="10"></property>
</bean>
</property>
</bean>
student类
package com.testdemo2;
public class Student {
public int id;
public Department department;//这里就表示一对一的关系,一个学生要对应一个部门
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
}
department类
package com.testdemo2;
public class Department {
public int d_id;
public int getD_id() {
return d_id;
}
public void setD_id(int d_id) {
this.d_id = d_id;
}
}
级联赋值
在配置外部bean之后,可以name=“department.d_id” 来直接配置 department对象中的d_id属性,注意department对象中一定要写 d_id的get方法
(在声明department对象的时候就可以给d_id属性赋值(比如下面,赋值d_id为10),为什么要用级联赋值呢)
<?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="student" class="com.testdemo2.Student">
<property name="id" value="10"></property>
<property name="department" ref="dept"></property>
<property name="department.d_id" value="100"/>
</bean>
<bean id="dept" class="com.testdemo2.Department">
<property name="d_id" value="10"/>
</bean>
</beans>
通过下面的方式 ,可以让不同的类共享同一个list集合(注意,开头的引用中增加了东西)
<?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: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="booklist">
<value> java从入门到精通</value>
<value> python从入门到精通</value>
<value> php从入门到精通</value>
</util:list>
<bean class="com.testdemo.Lession" id="lession">
<property name="name" value="bookname"></property>
<property name="books" ref="booklist"></property>
</bean>
<bean class="com.testdemo.Books" id="books">
<property name="books" ref="booklist"></property>
</bean>
</beans>
总结
写到这里也结束了,在文章最后放上一个小小的福利,以下为小编自己在学习过程中整理出的一个关于 java开发 的学习思路及方向。从事互联网开发,最主要的是要学好技术,而学习技术是一条慢长而艰苦的道路,不能靠一时激情,也不是熬几天几夜就能学好的,必须养成平时努力学习的习惯,更加需要准确的学习方向达到有效的学习效果。
由于内容较多就只放上一个大概的大纲,需要更及详细的学习思维导图的 点击我的Gitee获取。
还有 高级java全套视频教程 java进阶架构师 视频+资料+代码+面试题!
全方面的java进阶实践技术资料,并且还有技术大牛一起讨论交流解决问题。