我们可以动态地将值传递给Spring bean定义的参数吗?

时间:2015-11-20 12:16:22

标签: java spring

我想从属性文件中读取Spring bean定义中的参数值。可能吗 ?

示例: persistanceUnit arg的值,在下面的bean定义中我需要从属性文件中读取或以某种方式配置。

<bean id="profileDao" class="com.sample.dao.kundera.GenericKunderaDaoImpl">
              <constructor-arg name="persistanceUnit" type="java.lang.String" value="cds_test_pu"/>
              <constructor-arg name="persistentClass" type="java.lang.Class" value="com.sample.model.cds.Profile"/>
</bean>

2 个答案:

答案 0 :(得分:4)

如果spring已经获取了您的属性文件,您可以使用表达式来引用该属性:

   <div class="table-responsive">
    <nav ng-if="filterOn" class="navbar navbar-default">
        <form class="navbar-form">
 <div class="show-grid">
            <span class="input-group col-xs-4 col-md-4"> <span class="input-group-addon">Filter: </span>

                <input ng-model="filterOptions.filterText" class="form-control">
            </span>
            <span class="input-group col-xs-4 col-md-4"> <span class="input-group-addon" style="width: 20%">Filter A: </span>

                <select class="form-control" name="mySelect1" ng-model="sel1">
                    <option></option>
                    <option>{{selections}}</option>
                </select>
            </span>
            <span class="input-group col-xs-4 col-md-4"> <span class="input-group-addon">Filter B: </span>

                <select class="form-control" name="mySelect2" ng-model="nameVarTo">
                    <option></option>
                    <option>selections2</option>
                </select>
            </span>
  </div>
        </form>
    </nav>
</div>

Spring引导可以按惯例发现某些配置文件。在旧的xml配置日,您将使用<div style="margin: 10px 0 50px; float: left;">[one_fourth] <a href="https://midwesttinting.com/automotive-window-tinting/" target="_blank"> </a><a href="https://midwesttinting.com/automotive-window-tinting/" target="_blank"><img class="aligncenter wp-image-4713 size-full" src="https://midwesttinting.com/wp-content/uploads/2014/03/autotint.png" alt="Auto Window Tint Service Kansas City" width="160" height="160" /></a> <div style="float: left; text-align: center;"> <h3 style="margin: 0 auto; padding-bottom: 0;"><a title="Auto" href="#">Auto</a></h3> <p style="float: left; text-align: left;">Midwest Tinting offers a full line of automotive window tinting films, all of which can help reject up to 99% of the sun's damaging ultraviolet rays. [read_more text="Read More About Auto Window Tint" title="Auto Window Tint" url="http://midwesttinting.com/automotive-window-tinting/" align="right"]</p> </div> [/one_fourth] [one_fourth] <a href="https://midwesttinting.com/home-business-tint/" target="_blank"><img class="aligncenter wp-image-4718 size-full" src="https://midwesttinting.com/wp-content/uploads/2014/03/home.png" alt="Home Window Tint Service Kansas City" width="160" height="160" /></a> <div style="float: left; text-align: center;"> <h3 style="margin: 0 auto; padding-bottom: 0;"><a title="Home" href="#">Home</a></h3> <p style="float: left; text-align: left;">Whether your objective is to enhance beauty, increase comfort, increase security, or decrease utility costs, professionally installed window tint can help. [read_more text="Read More About Home Window Tint" title="Home Window Tint" url="http://midwesttinting.com/home-business-tint/" align="right"]</p> </div> [/one_fourth] [one_fourth] <a href="https://midwesttinting.com/home-business-tint/" target="_blank"><img class="aligncenter wp-image-4716 size-full" src="https://midwesttinting.com/wp-content/uploads/2014/03/business.png" alt="Business Window Tint Service Kansas City" width="160" height="160" /></a> <div style="float: left; text-align: center;"> <h3 style="margin: 0 auto; padding-bottom: 0;"><a title="Business Window Tint" href="#">Business</a></h3> <p style="float: left; text-align: left;">Save money! Glass windows and doors represent the most critical area of heat gain in the summer and heat loss for your business in the winter. [read_more text="Read More About Business Window Tint" title="Business Window Tint" url="http://midwesttinting.com/home-business-tint/" align="right"]</p> </div> [/one_fourth] [one_fourth_last] <a href="https://midwesttinting.com/paint-protection/" target="_blank"><img class="aligncenter wp-image-4717 size-full" src="https://midwesttinting.com/wp-content/uploads/2014/03/paint.png" alt="Auto Paint Protection Kansas City" width="160" height="160" /></a> <div style="float: left; text-align: center;"> <h3 style="margin: 0 auto; padding-bottom: 0;"><a title="Paint Protection" href="#">Paint Protection</a></h3> <p style="float: left; text-align: left;">Midwest Tinting proudly offers factory trained installation of XPEL Paint Protection Film – a virtually invisible shield that prevents damage to your vehicle. [read_more text="Read More About Paint Protection" title="XPEL Paint Protection" url="http://midwesttinting.com/xpel-paint-protection/" align="right"]</p> </div> [/one_fourth_last] [clear] [content_block bg_image="" max_bg_width="yes" bg_fixed="no" bg_position="center bottom" bg_repeat="repeat-x" parallax_scroll="no" bg_color="#293037" content_padding="30px 0 0" font_color="#FFFFFF" class="we-are-passionate-block"] <div style="text-align: center;"> <h4 style="margin-bottom: 25px;"><span style="font-size: 36px; color: #fd7800; line-height: 1.2;">Need a team you can count on? Look no further!</span></h4> <h5 style="margin-bottom: 20px;"><span style="font-size: 20px; color: #999999; line-height: 1.5;">Midwest Tinting's staff is the best around. We've been in the business since 1975 and continue to be #1 in customer service, product offering and workmanship.</span></h5> </div> <div style="font-size: 1.2em; color: #a9a9a9;"> <div class="one_half"> <h4 style="margin-bottom: 15px; color: #ffffff !important;"><strong>Why choose us?</strong></h4> <ul class="list-2" style="color: #999999; font-size: 17px !important; margin-top: 20px;"> <li>Our people are the best</li> <li>Our products are the best</li> <li>We take pride in our craft</li> <li>We stand behind every installation</li> <li>Awesome warranties</li> <li>International Window Film Association Accreditations <ul> <li>Brian - Master Accreditation Holder</li> <li>Caleb - Master Accreditation Holder</li> <li>Ron - Solar Control Specialist Accreditation Holder &amp; Automotive Film Specialist Accreditation Holder</li> </ul> </li> </ul> </div> <div class="one_half last_column" style="margin-bottom: 0;"><a href="https://midwesttinting.com/about-midwest-tinting/"><img class="aligncenter wp-image-4853 size-full" src="https://midwesttinting.com/wp-content/uploads/2014/03/mwtteampic.png" alt="Midwest Tinting Window Tint Service" width="500" height="333" /></a></div> </div> [/content_block] [content_block bg_image="" max_bg_width="yes" bg_fixed="yes" bg_position="center bottom" bg_repeat="no-repeat" parallax_scroll="no" bg_color="#ffffff" content_padding="40px 0 40px" font_color="#777777"] <div style="text-align: center;"> <h4 style="margin: 35px 0 25px;"><span style="font-size: 34px; color: #777777; line-height: 1.2;">Find Our Listings!</span></h4> <h5><span style="font-size: 18px; color: #999999; line-height: 1.5;">We care what our customers think! Here are some places we frequent on the web. We'd love to hear your thoughts, too!</span></h5> </div> [clear] [one_third] <div style="margin-top: 30px;"> <a href="http://www.iwfa.com"><img class="aligncenter wp-image-4557 size-medium" src="https://midwesttinting.com/wp-content/uploads/2014/03/IWFA-Revised-Logo-color-300x175.jpg" alt="International Window Film Association" width="300" height="175" /></a> </div> [/one_third] [one_third] <div style="margin-top: 30px;"> <a href="http://www.angieslist.com/companylist/us/ks/mission/midwest-tinting-reviews-115841.htm"><img class="aligncenter wp-image-4556" src="https://midwesttinting.com/wp-content/uploads/2014/03/SSA_Logo_250x250.png" alt="Angie's List Certified Midwest Tinting" width="200" height="200" /></a> </div> [/one_third] [one_third_last] <div style="margin-top: 30px;"> <a title="Click for the Business Review of Midwest Tinting, Inc., an Auto Service - Window Tinting in Shawnee Mission KS" href="http://www.bbb.org/kansas-city/business-reviews/auto-service-window-tinting/midwest-tinting-inc-in-shawnee-mission-ks-88060017#sealclick"><img class="alignnone" style="border: 0px;" src="https://seal-kansascity.bbb.org/seals/blue-seal-293-61-midwesttintinginc-88060017.png" alt="Midwest Tinting, Inc. is a BBB Accredited Business. Click for the BBB Business Review of this Auto Service - Window Tinting in Shawnee Mission KS" width="293" height="61" /></a> </div> [/one_third_last] <div style="float: left; display: block; height: 15px;"></div> [/content_block] [clear] [content_block bg_image="https://midwesttinting.com/wp-content/uploads/2014/03/parallax-bg.jpg" max_bg_width="yes" bg_fixed="yes" bg_position="center bottom" bg_repeat="no-repeat" parallax_scroll="no" bg_color="#24292E" content_padding="50px 0 55px" font_color="#EEEEEE"] <div style="text-align: center;"> <h4 style="margin-bottom: 20px;"><span style="font-size: 38px; color: #fff; line-height: 1;">Customer Reviews:</span></h4> <h4 style="margin-bottom: 20px;"><span style="font-size: 24px; color: #fff; line-height: 1;">Join the <span style="color: #f95700;">thousands </span>of happy Midwest Tinting customers and see why they <span style="color: #f95700;">love</span> us so much!</span></h4> <h5 style="margin-bottom: 10px;"><span style="font-size: 20px; color: #999; line-height: 1.5;"><em>"We spoke with Midwest Tinting on the phone and they were very helpful about explaining their product in detail. We were able to get an appointment within 24 hours. The process took about an hour and we left our car and came back later. VERY PLEASED with the professional job and the great look of the car! Midwest also gave us detailed instructions on the care of the windows."</em></span>  <span style="color: #ffffff; font-style: normal; font-weight: bold; font-size: 85%;">-- Angie's List Review</span></h5> </div> <div style="text-align: center; margin-top: 20px;"><a href="http://midwesttinting.com/testimonials/"><span style="font-size: 16px; font-style: italic;">More Customer Reviews...</span></a></div> [/content_block] <div style="margin-bottom: -35px;"> <p style="text-align: center;">[content_block bg_color="#FD7800" max_bg_width="yes" content_padding="35px 0" font_color="#FFFFFF" class="custom-padding"][slickr-flickr tag="autotint" thumbnail_size="small" align="center" descriptions="on" items="8"]</p> <h3 style="margin: 10px 0px; line-height: 1.2; color: #ffffff; font-size: 27px !important; text-align: center !important;">What Are You Waiting For? Get a Quote TODAY!</h3> [flat_button text="Contact Midwest Tinting" title="Get A Quote!" url="http://midwesttinting.com/contact/" padding="15px 20px" bg_color="transparent" border_color="#FFFFFF" border_width="2px" text_color="#FFFFFF" text_size="16px" align="center" target="_blank"] [/content_block] </div> </div>

引用配置文件
<bean id="profileDao" class="com.sample.dao.kundera.GenericKunderaDaoImpl">
              <constructor-arg name="persistanceUnit" type="java.lang.String" value="${cds_test_pu}"/>
              <constructor-arg name="persistentClass" type="java.lang.Class" value="com.sample.model.cds.Profile"/>
</bean>

Spring boot有很多关于配置外部化的功能 - 请看这里: http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config

答案 1 :(得分:0)

添加

  1. 您还可以使用Spring表达式语言SPEL在xml中的bean定义中动态解析值。
  2. 您还可以在应用程序启动时以编程方式将BeanDefinition注册到Spring BeanDefinitionRegistry。