发表于: 2018-04-02 23:01:18

1 756


一、今天完成的事情

 学习了简单的Spring,首先是xml配置文件

id和name

简单的说:id用来标识bean,是唯一的,且只有一个;name定义的是bean的alias,可以有多个,并可能与其他的bean重名。

详细的说:

id是唯一标识bean.不能用特殊字符:×#@ ,不能用数字开头。在bean引用的时候只能用id指向你需要的bean;

 

 

配置文件中不允许出现两个id相同的,否则在初始化时即会报错;但配置文件中允许出现两个name相同的,在用getBean()返回实例时,后面一个Bean被返回,应该是前面那个被后面同名的 覆盖了。有鉴于此,为了避免不经意的同名覆盖的现象,尽量用id属性而不要用name属性。


property后面紧跟着的name是类中对象的属性name



refvalue

 

1.ref引用一个已经存在的对象,value创建一个新的对象

2.value可以赋一些简单类型的值,ref可以引用其他的bean对象。

使用Spring进行了简单的输出

public class Category {
private int id;
   private String name;

   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 class Product {
private int id;
   private String name;
   private Category category;
//    @Autowired
   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;
   }
}


public class ProductTest {
public static void main(String[] args) {

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

       Product p = (Product) context.getBean("e");
       System.out.print(p.getName());
       System.out.println(p.getCategory().getName());
   }
}


<bean name="c" class="com.ptteng.pojo.Category">
   <property name="name" value="Spring!" />
</bean>
<bean name="e" class="com.ptteng.pojo.Product">
   <property name="name" value="Hello "/>
   <property name="category" ref="c"/>
</bean>

输出结果



二、明天计划的事情:

 继续学习Spring,尽快将JDBCTemplate完成

三、遇到的问题:

 对于很多基础的概念理解不透彻,打算明天好好再看看书,争取把最基础的概念吃透

四、收获:

 在网上看了很多的代码,对Spring和JDBCTemplate有了大概的认识,不再是一抹黑了

进度:慢慢慢慢慢

任务开始时间:201831

预计demo时间:2018420




返回列表 返回列表
评论

    分享到