发表于: 2017-06-16 21:58:36

2 951


今天完成的事情:
 没啥好写的,一直搞Spring
 学习几个关键字的使用:
 @Autowired自动装配
 @Resource (name="")资源
 @Component ("")组件

 <context:component-scan base-package"包名"/>

 贴点代码吧

package com.fanchen.code;

import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component("p")
public class Product {

private int id;
   private String name;

   @Resource(name="c")
private Category category;

   public int getId() {
return id;
   }

public void setId(int id) {
this.id = id;
   }

public String getName() {
return name="ppppppppppppppppp";
   }

public void setName(String name) {
this.name = name;
   }

public Category getCategory() {
return category;
   }
public void setCategory(Category category) {
this.category = category;
   }

}

package com.fanchen.code;

import org.springframework.stereotype.Component;

@Component
public class Category{
private int id;
   private String name;
   public void setId(int id){
this.id=id;
   }
public int getId(){
return id;
   }
public void setName(String name){
this.name=name;
   }
public String getName(){
return name="aaaaaaaaaaaa";
   }
}
package com.fanchen.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.fanchen.code.Product;

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());
   }
}



明天计划的事情:
 继续学习Spring
遇到的问题:
 没问题
收获:
 学会了上面写的几个关键词



返回列表 返回列表
评论

    分享到