注解与依赖注入
时间:2022-05-06
本文章向大家介绍注解与依赖注入,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
1.Product
package com.how2java.pojo;
import org.springframework.beans.factory.annotation.Autowired;
import javax.annotation.Resource;
import org.springframework.stereotype.*;
@Component("p")
public class Product {
private int id;
private String name="product 1";
// @Autowired
@Resource(name="c")
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;
}
// @Autowired
public void setCategory(Category category){
this.category=category;
}
}
2.Category
package com.how2java.pojo;
import org.springframework.stereotype.*;
@Component("c")
public class 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;
}
private int id;
private String name="category 1";
}
3. applicationContext.xml
<?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/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.how2java.pojo"/>
4. test case
package com.how2java.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.how2java.pojo.Product;
public class Test2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
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());
}
}
--------------------------------------------方法二、分割线-------------------------------------------------------------------------------------
package com.how2java.pojo;
import org.springframework.stereotype.*;
//@Component("c")
public class 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;
}
private int id;
private String name="category 1";
}
package com.how2java.pojo;
import org.springframework.beans.factory.annotation.Autowired;
import javax.annotation.Resource;
import org.springframework.stereotype.*;
//@Component("p")
public class Product {
private int id;
private String name="product 1";
// @Autowired
// @Resource(name="c")
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;
}
// @Autowired
public void setCategory(Category category){
this.category=category;
}
}
-----------------------------.xml-------------------------
<?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/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean name="c" class="com.how2java.pojo.Category">
<property name="name" value="category " />
<property name="id" value="1"/>
</bean>
<bean name="p" class="com.how2java.pojo.Product">
<property name="name" value="category " />
<property name="id" value="2" />
</bean>
</beans>
---------------------------------test--------------------------
package com.how2java.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.how2java.pojo.Category;
import com.how2java.pojo.Category1;
public class TestSpring {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
Category ef = (Category) context.getBean("c");
Category1 d = (Category1) context.getBean("d");
System.out.println(ef.getName());
System.out.println(ef.getId());
System.out.println(d.getId());
}
}
- MYSQL之库操作
- 实战-如何获取安卓iOS上的微信聊天记录、通过Metasploit控制安卓
- lightswitch binding custom control
- 3339: Rmq Problem
- Codeforce GYM 100741 A. Queries
- UVA - 11178 Morley's Theorem
- PyMySQL模块的使用
- Python之进程
- Angularjs 通过asp.net web api认证登录
- P3391 【模板】文艺平衡树(Splay)
- 零基础入门小程序 &实战经验分享
- mysql explain详解
- 【前沿】Pytorch开源VQA神经网络模块,让你快速完成看图问答
- #106. 二逼平衡树(附带详细代码注释)
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Django-admin配置和显示图标
- redis学习(八)
- 【剑指Offer】打印从1到最大的n位数
- 面试题-List之ArrayList、Vector、SynchronizedList、CopyOnWriteArrayList
- 面试题-JAVA设计模式之单例模式的5种实现方式
- 面试题-JAVA中的深拷贝、浅拷贝原理及实现
- Cross-Origin Resource Sharing (CORS)-跨域
- 多线程技术-CountDownLatch在业务中实践
- Mybatis源码阅读-准备环境搭建
- 【SpringBoot WebFlux 系列】WebFlux 之 Path 参数解析与 url 映射
- Oracle的expdp和impdp的使用方法
- python中的密度图与柱状图
- 单细胞数据(scRNAseq)可以做GSEA吗?
- mybatis-plus的collection、association 标签传递多参数
- 100 的阶乘末尾有多少个0?