bean管理指的是两件事:
1.spring创建对象
2.属性注入 给对象中的属性赋值
1.bean创建对象:
无参构造函数
xml文件:
属性值:id是唯一的标识 ,也别的也可以 ,class是对象所在的全路径 包名.类名
创建对象的时候,默认执行的是无参的构造函数,所以需要类中要有无参构造函数,否则会报错
有参构造函数
java如果定义了一个有参构造函数,则不会自动生成无参的构造函数,和c++一样,这时如果不想重新写无参构造函数,就想使用有参的构造函数,应该这样写
test类中生成user对象
这是通过绝对路径读取xml文件
ApplicationContext context =new FileSystemXmlApplicationContext("d:\\IdeaProjects\\untitled\\test_user.xml");
通过相对路径读取xml文件
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文件中 先配置对象的创建,再配置属性的注入
2)简化的set p名称空间注入
实际用的不多
1......在xml文件中进行属性注入
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/
2.....在bean中设置属性
3)通过xml文件设置属性的空值和特殊值
]]>
<北京> 是要放的特殊值 其他的是固定格式 也这样写就行了
4)通过xml方式注入集合类型属性
1.注入数组类型属性
2.注入list 集合类型
3.注入map 集合类型
4.注入 set集合类型
5.注入 list<自定义类> 集合类型
"java_array"
"python_array"
"c++_array"
"java_list"
"python_list"
"c++_list"
"set java"
"set python"
"set c++"
外部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类
这种写法是一样的,暂时未发现不同
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,也给这个属性赋值
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),为什么要用级联赋值呢)
通过下面的方式 ,可以让不同的类共享同一个list集合(注意,开头的引用中增加了东西)
java从入门到精通
python从入门到精通
php从入门到精通