Spring学习个人记录
创建第一个项目
1,添加依赖外部jar文件
2,在项目目录下先创建一个类Category,里面要对所有属性Name,Id等都设置对应的get和set函数。如setName(String name)和public String getName()。
3,在src文件夹下创建applicationContext.xml文件,在里面:
xmlns是xml命名空间,关于初始化bean的格式文件地址
xmlns:xsi:指xml所遵守的标签规范,辅助初始化bean
xsi:context:关于spring上下文,包括加载资源文件
xsi:schemaLocation:用于声明了目标名称空间的模式文档
<?xml version"1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
**重点要编写的:
<bean name="要创建的类的引用名" class="package名.类名">
<property name="属性名" value="要设置的值">
</bean>
在本例中,若在主函数创建对象引用为cat,上面则如下:
<bean name="cat" class="pojo.Category">
<property name="name" value="Fruit">
</bean>
上面这部分相当于set函数,可以根据自己的类的多少来设置多个,属性也是类似。上面相当于在属性名里面注入要设置的值。
<beans>
4,测试类
在主函数里面:
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[]{ "applicationContext.xml"});
//得到对象
// 类名 .xml文件里设置的类的引用名 = (类名)context.getBean(" .xml文件里设置的类//的引用名");
// 按照举例,也就是:
Category cat = (Category)context.getBean(“cat”);
cat,getName();//结果是Fruit
接下来引用就可调用对象的函数了。
在创建一个类时注入另一类的对象
注入时要使用ref
例如:在类Product 里面有属性Category category;想用引用cat来指向类对象。
Category类和Product类的package为:pojo,在主函数要创建的Product对象引用pro。设置如下:
<bean name="cat" class="pojo.Category">
<property Category类的属性的设置>
</bean>
按上面例子具体就是:
<bean name="pro" class="pojo.Product">
........
<property name="category" ref="cat"/>
....
</bean>
使用set函数
1,在主函数设置类属性时可以直接用类引用调用对应set函数,例如cat.setName(“Vegetable”);
2,设置对象属性时,则需要设置新的对象,以上面的两个类类举例。
<bean name="xxx" class="xxxx">
<property 设置同上>
</bean>
也就是:
<bean name="cat0" class="pojo.Category">
<property name="name" value="Vegetable">
</bean>
然后在主函数先创建该类对象:
Category cat0 = (Category)context.getBean(“cat0”);
在用调用set函数即可。
pro.setCategory(cat0);
调用pro.getCategory().getName()结果是Vegetable。
#具体代码
Category.java
package pojo;
/**
* @author TO BE BETTER AND HAPPY,YWW
* @date 2019/8/22 13:46
*/
public class Category {
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private int id;
private String name;
}
Product.java
package pojo;
/**
* @author TO BE BETTER AND HAPPY,YWW
* @date 2019/8/22 13:56
*/
public class Product {
private String name;
private String song;
private int love;
private Category cat;
public void setCat(Category ca){
this.cat = ca;
}
public void setName(String name){
this.name = name;
}
public void setSong(String song){
this.song = song;
}
public void setLove(int love){
this.love = love;
}
public Category getCat(){
return cat;
}
public String getName(){
return this.name;
}
public String getSong(){
return this.song;
}
public int getLove(){
return this.love;
}
}
TestSprig.java
package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import pojo.Category;
import pojo.Product;
/**
* @author TO BE BETTER AND HAPPY,YWW
* @date 2019/8/22 13:50
*/
public class TestSpring {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "applicationContext.xml" });
Category c11 = (Category) context.getBean("c11");
Product star = (Product) context.getBean("star");
star.setCat(c11);
System.out.println(star.getCat().getName());
star.setName("HuGe");
System.out.println("My favorite singer is " + star.getName() + " and his song is " + star.getSong());
System.out.println("My love for my family is " + star.getLove());
}
}
applicationContext.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-3.0.xsd">
<bean name="c" class="pojo.Category">
<property name="name" value="category 1" />
</bean>
<bean name="c11" class="pojo.Category">
<property name="name" value="category 11" />
</bean>
<bean name = "star" class="pojo.Product">
<property name="name" value="GD"/>
<property name="song" value="Butterfly"/>
<property name="love" value="10"/>
<property name="cat" ref="c"/>
</bean>
</beans>
结果如下: