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

    @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. 详细说明php的4中开源框架(TP,CI,Laravel,Yii)

    ThinkPHP简称TP,TP借鉴了Java思想,基于PHP5,充分利用了PHP5的特性,部署简单只需要一个入口文件,一起搞定,简单高效.中文文档齐全,入门超级简单.自带模板引擎,具有独特的数据验证和 ...

  2. docker 自制alpine-lnp镜像

    简单粗暴点吧 jenkins 镜像下载:docker pull jenkins:alpine dockfile 原地址:https://gist.github.com/phith0n/373cc078 ...

  3. September 19th 2017 Week 38th Tuesday

    Live boldly. Push yourself. Don't settle. 勇敢生活,突破自我,永不设限! Don't indulge in the past, whether it was ...

  4. mvc, web mvc, spring web mvc 区别

    1. MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用于组织代码用一种业务逻辑和数据显示分离的 ...

  5. 使用 CSS 根据兄弟元素的个数来调整样式

    在某些场景下,我们需要根据兄弟元素的总数来为它们设置样式.最常见的场景就是,当一个列表不断延长时,通过隐藏控件或压缩控件等方式来节省屏幕空间,以此提升用户体验. 为保证一屏内容能展示更多的内容,需要将 ...

  6. python第四课——运算符

    一.python中的运算符: 什么是运算符? 就是计算机语言中用来参与运算的符号!! 1.算数运算符: 符号:+ - * / %(取余,取模) //(取整) **(开方) 2.比较运算符: 特点:比较 ...

  7. springboot 配置jpa启动报Error processing condition on org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration.pageableCustomizer

    springboot +gradle 配置jpa启动报Error processing condition on org.springframework.boot.autoconfigure.data ...

  8. bootstrap 多级下拉菜单

    如上效果: 实现代码: 导入js和css: <link rel="stylesheet" href="http://cdn.static.runoob.com/li ...

  9. ZooKeeper(二)Java API使用

    ZooKeeper官网提供了Java和C的API. 本文使用Java API来实现ZooKeeper的基本操作. 前言 下图中的Replicated Database是包含完整数据树(entire d ...

  10. python redis 的基本操作指令

    #!/usr/bin/env python # -*- coding: utf-8 -*- ''' redis基本命令和基本用法详解 1.redis连接 2.redis连接池 3.redis基本命令 ...