spring 属性注入时,类中必须有setter 和 getter方法。

spring配置文件里:

java业务类中注入DAO:

	private StudentDao studentDao;
	// 通过属性注入
	public StudentDao getStudentDao() {
		return studentDao;
	}

	public void setStudentDao(StudentDao studentDao) {
		this.studentDao = studentDao;
	}

 

spring 构造方法注入时,要注明属性,类中不须要setter 和 getter方法,可是须要构造器对其进行赋值。

<!-- 定义studentService的bean实例 ,并注入studentDao -->
	<bean >		
		<constructor-arg ref="studentDao"></constructor-arg>
	</bean>

java业务类中注入DAO:

private StudentDao studentDao;

	// 通过属性注入

	public StudentDao getStudentDao() {
		return studentDao;
	}

	public void setStudentDao(StudentDao studentDao) {
		this.studentDao = studentDao;
	}

 

属性注入时,能够对其进行赋值,而且返回值由定义的属性值确定。

Java类中定义:

private long youknow;

	public long getYouknow() {
		return youknow;
	}

	public void setYouknow(long youknow) {
		this.youknow = youknow;
	}


 

<property name="youknow">
	<value>245</value>
</property>

或者直接这样写:

<property name="youknow" value="245"></property>


 

 

相关文章:

  • 2021-04-28
  • 2022-12-23
  • 2022-12-23
  • 2021-07-08
  • 2021-08-01
  • 2022-12-23
  • 2022-01-11
  • 2022-12-23
猜你喜欢
  • 2021-04-24
  • 2022-12-23
  • 2021-08-11
  • 2021-08-13
  • 2021-12-03
  • 2022-12-23
相关资源
相似解决方案