XML-based component declaration and injection


GitHub: https://github.com/SylkeWay/synthful

XSDs  required by Spring context configuration.

The Spring application context configuration file /durian/src/main/resources/spring/app-context.xml
<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"
    >

There are two XSD schemata required by the context configuration files.

Their placeholding URIs are
  • http://www.springframework.org/schema/beans
  • http://www.springframework.org/schema/context

xsi:schemaLocation is a mapping of placeholder URIs to XSD URLs.
URL, as opposed to URI, points to actual location of a resource.

The contents of xsi:schemaLocation must be in pairs, alternating between a URI and its XSD URL.

For example,
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd


Three forms of bean declaration in XML context configuration are exemplified here.
  • Configuring bean for a standard Java class as a bean, through its constructor.
  • Configuring bean for a class, through its constructor.
    • that has a single argument
    • using named arguments
    • depending on positional order
  • Configuring bean for a class, specifying a property.

Bean of a standard Java class, through its constructor

<bean id="greeting" class="java.lang.String">
  <constructor-arg type="java.lang.String" value="#### Hello Bellow ####" />
</bean>

This is akin to the declaration
String greeting = "#### Hello Bellow ####";

Bean for a class, through its constructor
<bean id="message" class="com.synthful.angst.app.durian.Message">
   <constructor-arg type="java.lang.String" value="Open" />
</bean>

This is akin to the writing the code
message = new Message("Open");

Bean for a class, through its constructor using named arguments
<bean id="hampdenME"
    class="com.synthful.angst.app.durian.Address">
    <constructor-arg type="java.lang.String" name="street" value="123 Main St " />
    <constructor-arg type="java.lang.String" name="city" value="Hampden" />
    <constructor-arg type="int" name="zip" value="4444" />
</bean>

Bean for a class, through its constructor using argument positional order
<bean id="hampdenME"
    class="com.synthful.angst.app.durian.Address">
    <constructor-arg type="java.lang.String" index="0" value="123 Main St " />
    <constructor-arg type="java.lang.String" index="1" value="Hampden" />
    <constructor-arg type="int" index="2" value="4444" />
</bean>


Bean for a class, by setting its properties
  <bean id="hampdenBranch" class="com.synthful.angst.model.Branch">
    <property name="manager" value="Sammy Davis"/>
    <property name="id" value="666"/>
    <property name="location" ref="hampdenME"/>
  </bean>


This is akin to  writing the Java code
Branch hampdenBranch = new Branch();
hampdenBranch.setManager("Sammy Davis");
hampdenBranch.setId = 666;
hampdenBranch.setLocation(hampdenME); 

Bean for a class, using constructor arguments and by setting its properties
<bean id="message" class="com.synthful.angst.app.durian.Message">
    <constructor-arg type="java.lang.String" value="לא אןרא רע" />
    <property name="suffix" value="כי אתה עמדי"/>
</bean>
As demonstrated before, if there are more than one constructor arguments, then named arguments or argument position should be used to identify the constructor arguments during component bean declaration.

Injection
These singleton components are injectable thro set{ClassInstanceProperty} methods or instance object declaration, where the ClassInstanceProperty or object name is the same as the bean id. There are two annotations for injecting beans
  • @Inject - exemplified below
  • @Resource - exemplified in the next page
@Resource annotation should be used where type identification is weak and bean identification is  solely possible on name identification, like injection of generics-encapsulated objects or JNDI resources.

Injecting Application Properties
Application properties can be injected thro the annotation

  • @Value(name="${applicationPropertyName}") 

Which will be discusses in more detail in the page on Context-variables Substitution During Maven Build.

@Service
public class AppInfo extends AAngsta {

    private Message message;
    private String wName;
    private int wValue;

    @Inject private Branch hampdenBranch;
    @Inject private String greeting;
    @Inject Address hampdenME;
    @Value(value = "${maxUsers}") String maxUsers;

    @Inject
    public void setMessage(Message message) {
        this.message = message;
        logger.info("message={}", message);
    }

    @Inject
    public void setWally(Wally wally) {
        this.wName = wally.getName();
        this.wValue = wally.getValue();

        // logger will print injected values
        // since this class is instantiated

        logger.info("After instantiation of: {}", this);
        logger.info("wally={}", wally);
        logger.info("message:{} Location={} {}", message, hampdenBranch, hampdenME);
        logger.info("property maxUsers: {}", maxUsers);
    }

    AppInfo() {

        // logger will print null injection values because
        // injection is possible only after this class is instantiated,
        // which is after constructor completes

        logger.info("message:{} Location={} {}", message, hampdenBranch, hampdenME);
    }
}



No comments:

Post a Comment