首页 技术 正文
技术 2022年11月12日
0 收藏 453 点赞 4,133 浏览 2728 个字

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

    @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;
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,490
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,905
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,738
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,491
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,129
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,292