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

    @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学习笔记 星球日two - 注解方式配置bean的更多相关文章

  1. spring学习笔记 星球日one - xml方式配置bean

    ide: idea lib包的导入:http://webcache.googleusercontent.com/search?q=cache:http://zyjustin9.iteye.com/bl ...

  2. Spring学习笔记(14)——注解零配置

    我们在以前学习  Spring  的时候,其所有的配置信息都写在  applicationContext.xml  里,大致示例如下: java代码: <beans> <bean n ...

  3. Spring学习笔记之依赖的注解(2)

    Spring学习笔记之依赖的注解(2) 1.0 注解,不能单独存在,是Java中的一种类型 1.1 写注解 1.2 注解反射 2.0 spring的注解 spring的 @Controller@Com ...

  4. 跟着刚哥学习Spring框架--通过注解方式配置Bean(四)

    组件扫描:Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件. 特定组件包括: 1.@Component:基本注解,识别一个受Spring管理的组件 2.@Resposit ...

  5. Spring框架学习(6)使用ioc注解方式配置bean

    内容源自:使用ioc注解方式配置bean context层 : 上下文环境/容器环境 applicationContext.xml 1 ioc注解功能 注解 简化xml文件配置 如 hibernate ...

  6. Spring 学习笔记(八)—— 注解使用整合

    @Autowired  —— 自动装配 需先在配置文件中,配置一个org.springframework.beans.factory.annotation. AutowiredAnnotationBe ...

  7. spring之通过注解方式配置Bean(一)

    (1)组件扫描:spring能够从classpath下自动扫描.侦测和实例化具有特定注解的组件. (2)特定组件包括: @Component:基本注解,标识一个受spring管理的组件: @Respo ...

  8. spring 注解方式配置Bean

    Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件特定组件包括: @Component:基本注解,标示了一个受Spring管理的Bean组件 @Respository:标识 ...

  9. Spring使用ioc注解方式配置bean

    context层 : 上下文环境/容器环境 applicationContext.xml 具体示例: 现在ioc容器中添加context层支持: 包括添加xmlns:context.xsi:schem ...

随机推荐

  1. SpringBoot Mybatis 执行自定义SQL

    1.XML中执行自定义SQL. https://blog.csdn.net/u012427355/article/details/80654806 2.注解执行自定义SQL @Select(" ...

  2. phpcas 整合 ecms的问题

    如果有ecms整合phpCAS经验的小伙伴可以给我留言,有偿付费 需求是:ecms登录自动跳转到CAS认证(已完成),认证后直接跳转到帝国cms系统中,并保持帝国cms系统权限等都和以前一样 正常使用 ...

  3. 解决Failed to load the JNI shared library xxx/xxx/jvm.dll 错误

    原因:jdk发生变化(新装了32位jdk),eclipse在启动时使用了 系统环境变量中的jdk路径(32位). 解决:只要把旧的64位的jre路径指定给eclipse启动文件即可. 在eclipse ...

  4. (1)网络编程的常识 (2)基于tcp协议的编程模型 (3)tcp协议和udp协议的比较 (4)基于udp协议的编程模型

    1.网络编程的常识 目前主流的网络通讯软件有:微信.QQ.YY.陌陌.探探.飞信.阿里旺旺.... 在吗? 1.1 七层网络模型(熟悉) 为了保证数据传递的可靠安全等等,ISO(国际标准委员会组织)将 ...

  5. ZT 内地20年经典电视剧大全

    内地20年经典电视剧大全     片尾曲:<故事就是故事> 演唱:戴娆 我听爷爷讲了一个故事 故事里的事是那昨天的事 故事里有好人也有坏人 故事里有好事也有坏事 故事里有多少是是非非 故事 ...

  6. Session攻击

    1.简介 Session对于Web应用无疑是最重要的,也是最复杂的.对于web应用程序来说,加强安全性的第一条原则就是 – 不要信任来自客户端的数据,一定要进行数据验证以及过滤,才能在程序中使用,进而 ...

  7. 跟我一起阅读Java源代码之HashMap(三)

    上一节我们讲到了如何用散列和链表实现HashMap,其中有一个疑问今天已经有些答案了,为什么要用链表而不是数组 链表的作用有如下两点好处 1. remove操作时效率高,只维护指针的变化即可,无需进行 ...

  8. ArcGis API for JavaScript学习——加载地图

    ArcGis API for JavaScript开发笔记——加载地图 在这个例子中使用的离线部署的API(请参见 http://note.youdao.com/noteshare?id=f42865 ...

  9. [国家集训队]小Z的袜子

    嘟嘟嘟 一眼就知道是莫队. 还不带修改,美滋滋. 按莫队的方法排序,然后用小学数学算一下概率,分子分母单独维护. #include<cstdio> #include<iostream ...

  10. 【转】[置顶] 在Android中显示GIF动画

    gif图动画在Android中还是比较常用的,比如像新浪微博中,有很多gif图片,而且展示非常好,所以我也想弄一个.经过我多方的搜索资料和整理,终于弄出来了,其实github上有很多开源的gif的展示 ...