19 Mayıs 2019 Pazar

XML ile Bean Tanımlama

Giriş
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 tag'i içinde kullanılacak namespace belirtilir. Şöyle yaparız.
<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>
alias tag
Şö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>
bean tag
Xml şöyledir.
<beans>
  <bean id="Foo" class="org.learnSpring.Foo">
    <property name="type" value="equilateral"></property>
  </bean>
</beans>
bean /constructor-arg tag
bean/factory method attribute
Açıklaması şöyle.
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.
Örnek
Şö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>
Şu çağrı ile ayndır.
Preferences.userNodeForPackage(MyController.class);
bean/depends-on attribute
Şö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>
bean/destroy-method attribute
Şöyle yaparız.
<bean id="executorService" 
  class="java.util.concurrent.Executors" 
  factory-method="newFixedThreadPool" 
  destroy-method="shutdown">
  <constructor-arg value="5"/>
</bean>
bean/class attribute
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"/>
Kod şöyledir.
interface MyCountryInterface {}
class USA implements MyCountryInterface {}
class UK implements MyCountryInterface ()
bean/abstract attribute
Abstract tanımlama yapmak istersek şöyle yaparız
<bean id="testBean" abstract="true"
  class="org.springframework.beans.TestBean">
</bean>
bean/property/list tag
Setter olarak kullanılır. Şöyle yaparız.
<property name="locations">  
  <list>
    <value>/WEB-INF/config/jdbc.properties</value>
  </list>  
</property>  
bean/property/props tag - java Properties Sınıfı
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>
bean/property/ref attribute - diğer beanler
Setter olarak kullanılır. Bean autowire olsun istersek şöyle yaparız.
<beans:bean class="com.service.Foo" id="Foo"  autowire="byType" />
Diğer bean'lere referans vermek için ref="..." şeklinde kullanırız. Şöyle yaparız.
<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>
bean/property/value attribute
Şöyle yaparız
<bean id="customer" class="com.napster.Customer">
  <property name="country" value="India" />
</bean>
util/list
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();
}
Şö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: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>
Diğer
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