Bean in spring configuration directly references the attribute values of other beans

Source: Internet
Author: User

The bean in the Pring configuration directly references the attribute values of other beans to assign values to the attributes of the current bean. You can also directly call other bean methods to obtain the returned values and assign values to the attributes of the current bean, in addition, parameters can be passed, eliminating the need to inject beans to obtain attribute values in beans.

The following is a complete example:

1. Java classes are required:

Bean in spring configuration directly references the property value package com of other beans. service. test; public class bean1 {int V1 = 1; Public int getv1 () {return V1;} public void setv1 (INT V1) {This. v1 = V1;} public int gettestvalue (INT V1) {return V1;} Public String gettestvalue2 (string Str) {return STR ;}} package COM. service. test; public class bean2 {int V1; int V2; int V3; string v4str; Public int getv1 () {return V1;} public void setv1 (INT V1) {This. v1 = V1;} public int getv2 () {return V2;} public void setv2 (INT V2) {This. v2 = V2;} public int getv3 () {return V3;} public void setv3 (INT V3) {This. v3 = V3;} Public String getv4str () {return v4str;} public void setv4str (string Str) {v4str = STR ;}} package COM. service. test; import Org. springframework. context. applicationcontext; import Org. springframework. context. support. classpathxmlapplicationcontext; public class test {/*** @ Param ARGs */public static void main (string [] ARGs) {// todo auto-generated method stubapplicationcontext context = new classpathxmlapplicationcontext ("testbean. XML "); bean2 bean2 = (bean2) context. getbean ("bean2"); system. out. println (bean2.getv1 (); system. out. println (bean2.getv2 (); system. out. println (bean2.getv3 (); system. out. println (bean2.getv4str ());}}

2. xml configuration is required:

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"><bean id="bean1" class="com.service.test.Bean1"></bean><bean id="bean2" class="com.service.test.Bean2"><property name="v1" value="#{bean1.v1}"/><property name="v2" value="#{bean1.getV1()}"/><property name="v3" value="#{bean1.getTestValue(3)}"/><property name="v4Str" value="#{'Hello '+bean1.getTestValue2('baby')}"/></bean></beans>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.