spring学习笔记 星球日two - 注解方式配置bean

注解要放在要注解的对象的上方

    @Autowired
private Category category;
<?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/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--<bean id="category" class="com.how2java.pojo.Category" aop:id="10" aop:name="kinome"></bean>-->
<!--&lt;!&ndash; Product 注入 Category对象 &ndash;&gt;-->
<!--<bean id="product" class="com.how2java.pojo.Product" aop:name="kinome_product" aop:category-ref="category"></bean>--> <!-- 注解方式注入对象 -->
<!--<context:annotation-config></context:annotation-config>-->
<!--<bean id="category" class="com.how2java.pojo.Category" aop:id="10" aop:name="kinome"></bean>-->
<!--<bean id="product" class="com.how2java.pojo.Product" aop:name="kinome_product"></bean>--> <!-- 完全注解方式配置bean -->
<!-- 这里指定了bean都在com.how2java.pojo路径下 -->
<context:component-scan base-package="com.how2java.pojo"/> </beans>
package com.how2java.pojo.test;

import com.how2java.pojo.Category;
import com.how2java.pojo.Product;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestSpring { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
// Category category = ctx.getBean(Category.class);
// Category category = (Category) ctx.getBean("category");
// System.out.println(category.getId()+" "+category.getName()); Product product = (Product) ctx.getBean("product");
System.out.println(product.getName()+" "+product.getCategory().getId()+" "+product.getCategory().getName()); } }
package com.how2java.pojo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import javax.annotation.Resource; @Component
public class Product {
private int id;
private String name="kinome_product"; // @Autowired // 这里指定了 category 为bean id
// @Resource(name = "category")
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;
}
}
package com.how2java.pojo;

import org.springframework.stereotype.Component;

@Component
public class Category {
private int id=10;
private String name="kinome"; 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;
}
}
上一篇:Spring中基于注解方式管理bean


下一篇:[Z] 关于c++ typename的另一种用法