发表于: 2018-10-18 23:28:17
1 506
一、今日完成:
通过运行TestSpring演示如何用Spring获取一个对象,并打印其name
1、写spring配置文件
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean name="c" class="cn.zjj.Category">
<property name="name" value="category 1" />
</bean>
写spring配置文件时,出错
这个意思是新建的Spring配置文件没有被加入到spring里面,添加就好了
2、写实体类
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;
}
3、写测试类
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSpring {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "applicationContext.xml" });
Category c = (Category) context.getBean("c");
System.out.println(c.getName());
}
}
控制台显示如下
对Product对象,注入一个Category对象
1、Product类
package cn.zjj;
public class Product {
private int id;
private String name;
private Category 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;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
}
变量可以这样定义吗
2、applicationContext.xml
在原配置中加入
<bean name="p" class="com.how2java.pojo.Product">
<property name="name" value="product1" />
<property name="category" ref="c" />
</bean>
3、测试
package test;
import cn.zjj.Product;
import cn.zjj.Category;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSpring {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "applicationContext.xml" });
Product p = (Product) context.getBean("p");
System.out.println(p.getName());
System.out.println(p.getCategory().getName());
}
}
控制台显示如下
使用注解的方式完成注入对象中的效果
1、在applicationContext.xml中添加<context:annotation-config/>
删除<!-- <property name="category" ref="c" /> -->
2、在Product.java的category属性前加上@Autowired注解,也可以在setCategory方法前加上@Autowired,效果相同(或者加上@Resource(name="c"))
3、运行测试
对Bean进行注解配置
1、修改applicationContext.xml,什么都去掉,只新增:<context:component-scan base-package="cn.zjj"/>
2、为Product类加上@Component注解,为Category 类加上@Component注解
,表明此类是bean,因为配置从applicationContext.xml中移出来了,所以属性初始化放在属性声明上进行。
3、运行测试
结果是一样的
二、明日计划:
继续学spring
三、问题:
写实体类的时候看到这个:private Category category;不是很懂
四、收获:
spring注解
评论