3、spring注解注入
1.写需要注解注入的类:
Propertie.java
package study;
public class Propertie {
public void show() {
System.out.print("我是注解注入的!");
}
}
2.Person接口:
package study;
public interface Person {
public void output();
}
package study;
//必要的包
import javax.annotation.Resource;
public class Chinese implements Person {
//注解注入符合@Resource
@Resource private Propertie p;
private String name;
public void output() {
p.show();
}
}
package study;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestPerson {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean3.xml");
Person p = (Person) ctx.getBean("chinese");
p.output();
}
}
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
<bean id="p" class="study.Propertie">
</bean>
<bean id="chinese" class="study.Chin
ese">
<property name="name">
<value>张敏鹏</value>
</property>
</bean>
</beans>
<!--
注解所引用的:
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=" http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
-->
6.运行结果:
转载于:https://www.cnblogs.com/zmpandzmp/p/3648811.html
3、spring注解注入相关推荐
- JAVA_OA管理系统(四)番外篇:使用Spring注解注入属性
本文介绍了使用Spring注解注入属性的方法.使用注解以前,注入属性通过类以及配置文件来实现.现在,注入属性可以通过引入@Autowired注解,或者@Resource,@Qualifier,@Pos ...
- Spring注解注入
spring注入方式-----注解注入 (1)操作: 首先在要注入的类前面加上: @Component(与后面三个是等价的)@Repository(持久层),@Service业务层,@Controll ...
- spring注解注入详解
1.Spring配置 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="ht ...
- spring注解注入IOC
1.创建Bean对象的注解 <1>配置自定义Bean对象注解 @Component.@Controller.@Service.@repository 用法:写在类上, 例如:@Servic ...
- 使用spring注解注入bean
使用配置文件时一般是这样写的 <bean id="person" class="com.spring.bean.Person" ><prope ...
- spring注解注入属性
转载于:https://www.cnblogs.com/hwgok/p/6751866.html
- Spring依赖注入:注解注入总结
更多11 spring 依赖注入 注解 java 注解注入顾名思义就是通过注解来实现注入,Spring和注入相关的常见注解有Autowired.Resource.Qualifier.Service.C ...
- 【Spring学习】spring依赖注入用法总结
顾名思义就是通过注解来实现注入,Spring和注入相关的常见注解有Autowired.Resource.Qualifier.Service.Controller.Repository.Componen ...
- 2.3.2 spring属性注入-注解注入-半注解注入-后序
代码: spring属性注入-注解注入-半注解后序.zip - 蓝奏云文件大小:15.2 K|https://www.lanzouw.com/iCjbVvpvxaf 上一个博客的半注解是,一个类在xm ...
最新文章
- 鸿蒙就是安卓换皮_被喷换皮安卓?华为手机鸿蒙OS Beta版今日上线
- c++作业5 9.22
- 【转】pdf 中如何把几页缩小成一页打印
- 建立适合大数据成功的团队
- 京东二面:MySQL 主从延迟、读写分离 7 种解决方案!
- MySQL 传统复制中常见故障处理和结构优化案例分析
- hyper-v 尝试更改 状态时 应用程序遇到错误 无法初始化
- 后副车架焊接机器人_焊接机器人的工装设计和工装的使用方法
- java获取当月1号 的时间chuo_java获取时间戳的方法
- Kmalloc申请内存源码分析
- cad2010多个文件并排显示_CAD2010中如何将多个文档并排显示在一个窗口里?
- matlab 振动,振动系统固有特性的matlab计算
- PMP考试多少分算通过?
- 【开发工具集】显示设备驱动程序列表——DriverView
- DataCastle[猜你喜欢]赛事算法分享
- 【高并发】如何解决可见性和有序性问题?这次彻底懂了!
- Springboot启动提示:com.alibaba.nacos.shaded.io.grpc.StatusRuntimeException UNAVAILAB
- 【龙印】步进电机使用七段式抛物线型S曲线加减速和路径规划的理论分析和实现
- nachos操作系统(一)
- GameNFT深度分析, 打造属于头号玩家的开放世界!