在Spring配置文件bean标签中的autowire属性的no参数
指:不适用自动装配,只是用ref进行装配注入
案例:使用autowire=“no”
1、创建类StudentServiceImpl,代码如下:
package cn.zd.service;
public class StudentServiceImpl {
private String studentName;
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getStudentName() {
return studentName;
}
public void say(){
System.out.println("学生的名字是:"+studentName);
}
}
注意:此类中有一个私有属性studentName,并且必须有这个属性的setter方法
2、创建类TeacherServiceImpl,代码如下:
package cn.zd.service;
public class TeacherServiceImpl {
private String teacherName;
public StudentServiceImpl studentServiceImpl;
public void setTeacherName(String teacherName) {
this.teacherName = teacherName;
}
public void setStudentServiceImpl(StudentServiceImpl studentServiceImpl) {
this.studentServiceImpl = studentServiceImpl;
}
public String getTeacherName() {
return teacherName;
}
public void say(){
System.out.println("老师的名字是:"+teacherName);
System.out.println(teacherName+"老师的学生是:"+studentServiceImpl.getStudentName());
}
}
注意:这个类中需要引用StudentServiceImpl 类,并且同样需要有setter方法
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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="studentServiceImpl" class="cn.zd.service.StudentServiceImpl">
<property name="studentName">
<value>张迪</value>
</property>
</bean>
<!-- TeacherServiceImpl这个类中因为使用了autowire="no"参数,
所以当引用StudentServiceImpl这个类时就要使用ref属性来指明引用的对象-->
<bean id="teacherServiceImpl" class="cn.zd.service.TeacherServiceImpl" autowire="no">
<property name="teacherName">
<value>陈</value>
</property>
<property name="studentServiceImpl">
<ref bean="studentServiceImpl"/>
</property>
</bean>
</beans>
注意:TeacherServiceImpl这个类中因为使用了autowire="no"参数,所以当引用StudentServiceImpl这个类时就要使用ref属性来指明引用的对象,ref中的参数为所引用的对象的id名。
4、测试类,代码如下:
package cn.zd.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.zd.service.TeacherServiceImpl;
public class TestNO {
@Test
public void test1(){
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:app*.xml");
TeacherServiceImpl teacherServiceImpl = (TeacherServiceImpl) ac.getBean("teacherServiceImpl");
teacherServiceImpl.say();
}
}
5、运行结果:
老师的名字是:陈
陈老师的学生是:张迪
------------------------------------------------------------
以上为自我理解,若有不足,请高手指点,谢谢.....
分享到:
相关推荐
Spring自动装配的方式和举例、以及@Qualifier、@Autowire、@Resource的使用。
主要介绍了Spring实战之@Autowire注解用法,结合实例形式详细分析了Spring @Autowire注解具体实现步骤与相关使用技巧,需要的朋友可以参考下
在idea2020 下写的spring5-autowired 小例子。person,cat dog ,给大家一个参考。
NULL 博文链接:https://garrincha.iteye.com/blog/2109488
1.autowire=defualt 2.autowire=“byName” 3.autowire=bytype 详细解析注入方式 例如:有如下两个类需要注入 第一个类: 1.package org.jia; 2. 3. public class Order { 4. ...
<bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <ref bean="runTime" /> </bean>
#学习springboot自动装配 ##一,手动装配 ### 1,模式注解装配 @Component注解,或者@Component注解的扩展,@ Controller,@ Service,存储库,@ Configruation等, ### 2. @ Configuration启动容器+ @ Bean注册...
NULL 博文链接:https://diaochenlong2.iteye.com/blog/1831333
13.@Autowire注解与自动装配 14.让Spring自动扫描和管理Bean 15.使用JDK中的Proxy技术实现AOP功能 16.使用CGLIB实现AOP功能与AOP概念解释 17.使用Spring的注解方式实现AOP入门 18.使用Spring的注解方式实现AOP...
使用autowire属性指定自动装配的方式byName根据bean的名字和当前bean的setter风格属性名进行自动装配若有匹配,则自动转配若无匹配,则不装配
以Spring5.3.6为演示基础,以多个范例显示演示了autowire多种形式的应用 default、byName、byType、constructor 并对相关使用做了一定的对比
autowire自动导入句柄
实现了简单的ORM增删改查。 ... abstract="false" lazy-init="default" autowire="default" dependency-check="default"> <property name="dataSource"> <ref local="dataSource" /> </bean>
<bean name="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <ref bean="myJobTrigger" /> ...
主要介绍了Idea 解决 Could not autowire. No beans of 'xxxx' type found 的错误提示,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
八、 自动装配autowire 13 (一) byName 13 (二) byType 14 (三) 注意 14 九、 生命周期 15 (一) lazy-init/default-lazy-init 15 (二) init-method destroy-method 不要和prototype一起用(了解) 15 第六课:...
内容如下: spring.rar [spring_aop1] [spring_aop2] [spring_aop3] [spring_aop4] [spring_autowire_byName] [spring_autowire_byType] [spring_beginning] [spring_hibernate_1] [spring_hibernate_2] ...
<bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <ref bean="getPolicyTime"/> </beans>
Spring系列第7篇:依赖注入之手动注入Spring系列第8篇:自动注入(autowire)详解,高手在于坚持Spring系列第9篇:depend-on到底是干什么的?Spring系列第10篇:primary可以解决什么问题?Spring系列第11篇:bean中...