Giriş
bean tanımlarken kullanılan class ismi her zaman fully qualified class name olmalıdır
beans tag'i
Şöyle yaparız.
Şöyle yaparız.
Xml şöyledir.bean tanımlarken kullanılan class ismi her zaman fully qualified class name olmalıdır
beans tag'i
Şöyle yaparız.
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
  bean-discovery-mode="annotated"><beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xmlns:security="http://www.springframework.org/schema/security"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/security
    http://www.springframework.org/schema/security/spring-security.xsd">    
</beans>Şöyle yaparız.
<beans xmlns="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.xsd">
  <alias name="appDataSource" alias="jpaDataSource" />    
  <alias name="jpaTransactionManager" alias="appTransactionManager" />
  <bean id="appDataSource" >
    ...
  </bean>
  <bean id="entityManagerFactory"
    ...
  </bean>
</beans><beans>
  <bean id="Foo" class="org.learnSpring.Foo">
    <property name="type" value="equilateral"></property>
  </bean>
</beans>bean/factory method attribute
Açıklaması şöyle.
Örnek
Şöyle yaparız.
Arguments to the static factory method are supplied via elements, exactly the same as if a constructor had actually been used. The type of the class being returned by the factory method does not have to be of the same type as the class that contains the static factory method, although in this example it is. An instance (non-static) factory method would be used in an essentially identical fashion (aside from the use of the factory-bean attribute instead of the class attribute), so details will not be discussed here.
Şöyle yaparız.
<bean id="preferences" class="java.util.prefs.Preferences"
  factory-method="userNodeForPackage">
    <constructor-arg type="java.lang.Class" value="com.path.MyController" />
</bean>Şöyle yaparız.
<bean id="partnershipPluginService" class="com.foo.Bar" lazy-init="false">
  <property name="selectionService" ref="selectionService"/>
  <property name="editObjectService" ref="editObjectService"/>
  <property name="securityFieldsService" ref="securityFieldsService"/>
  <property name="cryptoSettingsService" ref="cryptoSettingsService"/>
  <property name="authenticationService" ref="systemAuthenticationService"/>
  <property name="configurationManager" ref="serverConfigurationManager"/>
  <property name="lifeCycleDefService" ref="lifeCycleDefService"/>
</bean>
<bean class="com.foo.Baz" depends-on="partnershipPluginService" lazy-init="false">
  <property name="registryPort" value="${rmi.port}"/>
  <property name="serviceName" value="partnershipPluginService"/>
  <property name="service" ref="partnershipPluginService"/>
</bean>Şöyle yaparız.
<bean id="executorService" 
  class="java.util.concurrent.Executors" 
  factory-method="newFixedThreadPool" 
  destroy-method="shutdown">
  <constructor-arg value="5"/>
</bean>Kalıtım hiyerarşisi kullanmak istersek şöyle yaparız. Arayüz için id tanımlamaya gerek yoktur.
<bean class="MyCountryInterface"/>
<bean id="country" class="UK"/>
<bean id="main" class="Main"/>interface MyCountryInterface {}
class USA implements MyCountryInterface {}
class UK implements MyCountryInterface ()Abstract tanımlama yapmak istersek şöyle yaparız
<bean id="testBean" abstract="true"
  class="org.springframework.beans.TestBean">
</bean>Setter olarak kullanılır. Şöyle yaparız.
<property name="locations">  
  <list>
    <value>/WEB-INF/config/jdbc.properties</value>
  </list>  
</property>  Setter olarak kullanıılır. Şöyle yaparız.
<bean id="id1"
  class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
  <property name="mappings">
    <props>
      <prop key="login.do">id2</prop>
    </props>
  </property>
</bean>Setter olarak kullanılır. Bean autowire olsun istersek şöyle yaparız.
<beans:bean class="com.service.Foo" id="Foo"  autowire="byType" /><beans>
  <bean id="triangle" class="org.tutorial.spring.Triangle">
    <property name="pointB" ref="pointB"/>
    <property name="pointC" ref="pointC"/>
  </bean>
  <bean id="pointA" class="org.tutorial.spring.Point">
    <property name="x" value="0"/>
    <property name="y" value="0"/>
  </bean>
  <bean id="pointB" class="org.tutorial.spring.Point">
    <property name="x" value="100"/>
    <property name="y" value="200"/>
  </bean>
</beans>Şöyle yaparız
<bean id="customer" class="com.napster.Customer">
  <property name="country" value="India" />
</bean>Bean'ler kendi sınıfımız olabileceği gibi List of X tipinden de olabilir.
Örnek
List tipinden bir bean yaratmak isteyelim. Yani şu kodu isteyelim
@Bean
public List<SomeType> someTypesFactory(){
   return Collections.emtpyList();
}<?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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                    http://www.springframework.org/schema/util
                    http://www.springframework.org/schema/util/spring-util-2.5.xsd">
<util:list id="someTypesList" value-type="com.project.SomeType">
</util:list>context/property-placeholder attribute
Şöyle yaparız.
<?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:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context.xsd">
  <context:property-placeholder location="another.properties" />
  <!-- this property value is defined in another.properties-->
  <bean class="demo.Foo" >
    <property name="name" value="${name.property}"/>
  </bean>
  <!-- this property value is defined in application.properties-->
  <bean class="demo.Foo" >
    <property name="name" value="${some.property}"/>
  </bean>
</beans> 
Hiç yorum yok:
Yorum Gönder