🗊Презентация 2. Java Spring Core 3. Spring IoC Container

Нажмите для полного просмотра!
2. Java Spring Core 3. Spring IoC Container, слайд №12. Java Spring Core 3. Spring IoC Container, слайд №22. Java Spring Core 3. Spring IoC Container, слайд №32. Java Spring Core 3. Spring IoC Container, слайд №42. Java Spring Core 3. Spring IoC Container, слайд №52. Java Spring Core 3. Spring IoC Container, слайд №62. Java Spring Core 3. Spring IoC Container, слайд №72. Java Spring Core 3. Spring IoC Container, слайд №82. Java Spring Core 3. Spring IoC Container, слайд №92. Java Spring Core 3. Spring IoC Container, слайд №102. Java Spring Core 3. Spring IoC Container, слайд №112. Java Spring Core 3. Spring IoC Container, слайд №122. Java Spring Core 3. Spring IoC Container, слайд №132. Java Spring Core 3. Spring IoC Container, слайд №142. Java Spring Core 3. Spring IoC Container, слайд №152. Java Spring Core 3. Spring IoC Container, слайд №162. Java Spring Core 3. Spring IoC Container, слайд №172. Java Spring Core 3. Spring IoC Container, слайд №182. Java Spring Core 3. Spring IoC Container, слайд №192. Java Spring Core 3. Spring IoC Container, слайд №202. Java Spring Core 3. Spring IoC Container, слайд №212. Java Spring Core 3. Spring IoC Container, слайд №222. Java Spring Core 3. Spring IoC Container, слайд №232. Java Spring Core 3. Spring IoC Container, слайд №242. Java Spring Core 3. Spring IoC Container, слайд №252. Java Spring Core 3. Spring IoC Container, слайд №262. Java Spring Core 3. Spring IoC Container, слайд №272. Java Spring Core 3. Spring IoC Container, слайд №282. Java Spring Core 3. Spring IoC Container, слайд №292. Java Spring Core 3. Spring IoC Container, слайд №302. Java Spring Core 3. Spring IoC Container, слайд №312. Java Spring Core 3. Spring IoC Container, слайд №322. Java Spring Core 3. Spring IoC Container, слайд №332. Java Spring Core 3. Spring IoC Container, слайд №342. Java Spring Core 3. Spring IoC Container, слайд №352. Java Spring Core 3. Spring IoC Container, слайд №362. Java Spring Core 3. Spring IoC Container, слайд №372. Java Spring Core 3. Spring IoC Container, слайд №382. Java Spring Core 3. Spring IoC Container, слайд №392. Java Spring Core 3. Spring IoC Container, слайд №402. Java Spring Core 3. Spring IoC Container, слайд №412. Java Spring Core 3. Spring IoC Container, слайд №422. Java Spring Core 3. Spring IoC Container, слайд №432. Java Spring Core 3. Spring IoC Container, слайд №442. Java Spring Core 3. Spring IoC Container, слайд №452. Java Spring Core 3. Spring IoC Container, слайд №462. Java Spring Core 3. Spring IoC Container, слайд №472. Java Spring Core 3. Spring IoC Container, слайд №482. Java Spring Core 3. Spring IoC Container, слайд №492. Java Spring Core 3. Spring IoC Container, слайд №502. Java Spring Core 3. Spring IoC Container, слайд №512. Java Spring Core 3. Spring IoC Container, слайд №522. Java Spring Core 3. Spring IoC Container, слайд №532. Java Spring Core 3. Spring IoC Container, слайд №542. Java Spring Core 3. Spring IoC Container, слайд №552. Java Spring Core 3. Spring IoC Container, слайд №562. Java Spring Core 3. Spring IoC Container, слайд №572. Java Spring Core 3. Spring IoC Container, слайд №582. Java Spring Core 3. Spring IoC Container, слайд №592. Java Spring Core 3. Spring IoC Container, слайд №602. Java Spring Core 3. Spring IoC Container, слайд №612. Java Spring Core 3. Spring IoC Container, слайд №622. Java Spring Core 3. Spring IoC Container, слайд №632. Java Spring Core 3. Spring IoC Container, слайд №642. Java Spring Core 3. Spring IoC Container, слайд №652. Java Spring Core 3. Spring IoC Container, слайд №662. Java Spring Core 3. Spring IoC Container, слайд №672. Java Spring Core 3. Spring IoC Container, слайд №682. Java Spring Core 3. Spring IoC Container, слайд №692. Java Spring Core 3. Spring IoC Container, слайд №702. Java Spring Core 3. Spring IoC Container, слайд №712. Java Spring Core 3. Spring IoC Container, слайд №722. Java Spring Core 3. Spring IoC Container, слайд №732. Java Spring Core 3. Spring IoC Container, слайд №74

Содержание

Вы можете ознакомиться и скачать презентацию на тему 2. Java Spring Core 3. Spring IoC Container. Доклад-сообщение содержит 74 слайдов. Презентации для любого класса можно скачать бесплатно. Если материал и наш сайт презентаций Mypresentation Вам понравились – поделитесь им с друзьями с помощью социальных кнопок и добавьте в закладки в своем браузере.

Слайды и текст этой презентации


Слайд 1





2. Spring Core
3. Spring IoC Container
Описание слайда:
2. Spring Core 3. Spring IoC Container

Слайд 2





The Spring Container
The container will:
 create the objects
 wire them together
 configure them
 manage their complete lifecycle from creation till destruction. 
These objects are called Spring Beans
Описание слайда:
The Spring Container The container will: create the objects wire them together configure them manage their complete lifecycle from creation till destruction. These objects are called Spring Beans

Слайд 3





Dependency Injection
The Spring container uses dependency injection (DI) to manage the components
In a complex Java application classes should be as independent as possible to increase the possibility to reuse these  classes and  to  test  them  independently
Dependency Injection helps in connecting  these classes together and same time keeping them independent.
Описание слайда:
Dependency Injection The Spring container uses dependency injection (DI) to manage the components In a complex Java application classes should be as independent as possible to increase the possibility to reuse these classes and to test them independently Dependency Injection helps in connecting these classes together and same time keeping them independent.

Слайд 4





DI Implementation
Описание слайда:
DI Implementation

Слайд 5





Container’s Metadata
The  container  gets  its  instructions  on  what  objects  to  instantiate,  configure,  and  assemble  by reading configuration metadata provided:
by  XML
Java  annotations
Java  code
Описание слайда:
Container’s Metadata The container gets its instructions on what objects to instantiate, configure, and assemble by reading configuration metadata provided: by XML Java annotations Java code

Слайд 6





 How Spring Works
Описание слайда:
How Spring Works

Слайд 7





Spring Bean Definition
The objects that form the backbone of  your application and that are managed by the Spring IoC container are called beans
The bean definition contains the information called configuration metadata which is needed for the container to know:
How to create a bean
Bean's lifecycle details
Bean's dependencies
Описание слайда:
Spring Bean Definition The objects that form the backbone of your application and that are managed by the Spring IoC container are called beans The bean definition contains the information called configuration metadata which is needed for the container to know: How to create a bean Bean's lifecycle details Bean's dependencies

Слайд 8





XML Metadata
<?xml version="1.0" encoding="UTF-8"?>
<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-3.0.xsd">
   <!-- Bean declarations go here -->
</beans>
Описание слайда:
XML Metadata <?xml version="1.0" encoding="UTF-8"?> <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-3.0.xsd"> <!-- Bean declarations go here --> </beans>

Слайд 9





Spring Bean Definition
Each bean definition can contain a set of the following properties:
Class
Name
Scope
constructor-arg
Properties
autowiring mode
lazy-initialization mode
initialization method
destruction method
Описание слайда:
Spring Bean Definition Each bean definition can contain a set of the following properties: Class Name Scope constructor-arg Properties autowiring mode lazy-initialization mode initialization method destruction method

Слайд 10





Bean Definition Attributes
Class attribute is mandatory and specify the bean class to be used to create the bean
Name attribute specifies the bean identifier uniquely. In XML-based configuration metadata, you use the id and/or name attributes to specify the bean identifier(s)
Properties is used to inject the dependencies
Описание слайда:
Bean Definition Attributes Class attribute is mandatory and specify the bean class to be used to create the bean Name attribute specifies the bean identifier uniquely. In XML-based configuration metadata, you use the id and/or name attributes to specify the bean identifier(s) Properties is used to inject the dependencies

Слайд 11





Spring Tool Suite Installation
Open Eclipse -> Help -> Eclipse Marketplace…
Find = STS -> click Find button -> select STS for Eclipse Luna -> Install button -> Confirm -> I accept -> Finish
Restart Eclipse
Описание слайда:
Spring Tool Suite Installation Open Eclipse -> Help -> Eclipse Marketplace… Find = STS -> click Find button -> select STS for Eclipse Luna -> Install button -> Confirm -> I accept -> Finish Restart Eclipse

Слайд 12





Example 1. Hello World in Spring
Create Spring project
Tune project for logging
Create project classes (POJOs)
Create Spring metadata
Create application context
Run application
Описание слайда:
Example 1. Hello World in Spring Create Spring project Tune project for logging Create project classes (POJOs) Create Spring metadata Create application context Run application

Слайд 13





Example 1. Create Spring Project
File -> New -> Other.. -> Spring -> Spring project -> Next
Project name = P211BeanDefinition, Templates = Simple Spring Utility Project -> Next
Package = com.bionic.edu -> Finish
Описание слайда:
Example 1. Create Spring Project File -> New -> Other.. -> Spring -> Spring project -> Next Project name = P211BeanDefinition, Templates = Simple Spring Utility Project -> Next Package = com.bionic.edu -> Finish

Слайд 14





Example 1. Create Spring Project
Remove from com.bionic.edu package (src/main/java) template files Service.java and ExampleService.java
Remove from com.bionic.edu package (src/test/java) template files ExampleConfigurationTests.java and ExampleServiceTests.java
Описание слайда:
Example 1. Create Spring Project Remove from com.bionic.edu package (src/main/java) template files Service.java and ExampleService.java Remove from com.bionic.edu package (src/test/java) template files ExampleConfigurationTests.java and ExampleServiceTests.java

Слайд 15





Example 1. Logging Dependencies
Описание слайда:
Example 1. Logging Dependencies

Слайд 16





Add Dependencies to the pom.xml File
Open pom.xml -> Dependencies tab -> select junit: 3.8.1[test] -> Properties -> change version to 4.7 -> Ok
Press Add button -> GroupId = org.slf4j, ArifactId = slf4j-api, Vesion = 1.7.5 -> Ok
Press Add button -> GroupId = org.apache. logging.log4j, ArtifactId = log4j-api, Version = 2.0.2 -> Ok 
And so on… Then -> Save
Описание слайда:
Add Dependencies to the pom.xml File Open pom.xml -> Dependencies tab -> select junit: 3.8.1[test] -> Properties -> change version to 4.7 -> Ok Press Add button -> GroupId = org.slf4j, ArifactId = slf4j-api, Vesion = 1.7.5 -> Ok Press Add button -> GroupId = org.apache. logging.log4j, ArtifactId = log4j-api, Version = 2.0.2 -> Ok And so on… Then -> Save

Слайд 17





Further pom.xml Tuning
Select log4j (1.2.14) dependency and click Remove button
Go to pom.xml tag and change 1.5 Java version to 1.8 both in <source> and <target> tags of maven-compiler-plugin artefact
Change Spring version to 4.1.1.RELEASE in <spring.framework.version> tag
Save pom.xml file
Описание слайда:
Further pom.xml Tuning Select log4j (1.2.14) dependency and click Remove button Go to pom.xml tag and change 1.5 Java version to 1.8 both in <source> and <target> tags of maven-compiler-plugin artefact Change Spring version to 4.1.1.RELEASE in <spring.framework.version> tag Save pom.xml file

Слайд 18





Maven clean&install actions
Right click on the project’s name -> Maven -> Update project -> Ok
Right click on the project’s name -> Run As… -> Maven clean
Right click on the project’s name -> Run As… -> Maven install
Описание слайда:
Maven clean&install actions Right click on the project’s name -> Maven -> Update project -> Ok Right click on the project’s name -> Run As… -> Maven clean Right click on the project’s name -> Run As… -> Maven install

Слайд 19





Example 1. Hello World in Spring
Create Spring project
Tune project for logging
Create project classes (POJOs)
Create Spring metadata
Create application context
Run application
Описание слайда:
Example 1. Hello World in Spring Create Spring project Tune project for logging Create project classes (POJOs) Create Spring metadata Create application context Run application

Слайд 20





Example 1. log4j2.xml in resources
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="WARN">
    <appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level 
		%logger{36} - %msg%n"/>
        </Console>
    </appenders>
    <loggers>
        <root level="warn">
            <appender-ref ref="Console"/>
        </root>
    </loggers>
</configuration>
Описание слайда:
Example 1. log4j2.xml in resources <?xml version="1.0" encoding="UTF-8"?> <configuration status="WARN"> <appenders> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/> </Console> </appenders> <loggers> <root level="warn"> <appender-ref ref="Console"/> </root> </loggers> </configuration>

Слайд 21





Example 1. Hello World in Spring
Create Spring project
Tune project for logging
Create project classes (POJOs)
Create Spring metadata
Create application context
Run application
Описание слайда:
Example 1. Hello World in Spring Create Spring project Tune project for logging Create project classes (POJOs) Create Spring metadata Create application context Run application

Слайд 22





E1. GreetingService Interface
package com.bionic.edu;
 
public interface GreetingService {
	void sendGreeting();
}
Описание слайда:
E1. GreetingService Interface package com.bionic.edu;   public interface GreetingService { void sendGreeting(); }

Слайд 23





E1. HelloWorldService Class
package com.bionic.edu;
 
public class HelloWorldService implements 
		GreetingService {
     public void sendGreeting() {
	  System.out.println("Hello, world!");
     }
}
Описание слайда:
E1. HelloWorldService Class package com.bionic.edu;   public class HelloWorldService implements GreetingService { public void sendGreeting() { System.out.println("Hello, world!"); } }

Слайд 24





E1. HelloKittyService Class
package com.bionic.edu;
 
public class HelloKittyService implements 			GreetingService {
     public void sendGreeting(){
	   System.out.println("Hello, Kitty!");
     }
}
Описание слайда:
E1. HelloKittyService Class package com.bionic.edu;   public class HelloKittyService implements GreetingService { public void sendGreeting(){ System.out.println("Hello, Kitty!"); } }

Слайд 25





Example 1. Hello World in Spring
Create Spring project
Tune project for logging
Create project classes (POJOs)
Create Spring metadata
Create application context
Run application
Описание слайда:
Example 1. Hello World in Spring Create Spring project Tune project for logging Create project classes (POJOs) Create Spring metadata Create application context Run application

Слайд 26





Bean Definition
The <bean> element tells Spring to create an object for you. 
The id attribute gives the bean a name by which it’ll be referred to in the Spring container. 
When the Spring container loads its beans, it’ll instantiate the bean using the default constructor.
Описание слайда:
Bean Definition The <bean> element tells Spring to create an object for you. The id attribute gives the bean a name by which it’ll be referred to in the Spring container. When the Spring container loads its beans, it’ll instantiate the bean using the default constructor.

Слайд 27





Example 1. Bean Definition
<bean id="helloWorldService" class="com.bionic.edu.HelloWorldService" />
Описание слайда:
Example 1. Bean Definition <bean id="helloWorldService" class="com.bionic.edu.HelloWorldService" />

Слайд 28





Example 1. Configuration File
Right click on src/main/resources -> New  -> File
Fill File name with configuration file Id (beans.xml) -> Finish
Create configuration file context (see next slide) -> Save
Описание слайда:
Example 1. Configuration File Right click on src/main/resources -> New -> File Fill File name with configuration file Id (beans.xml) -> Finish Create configuration file context (see next slide) -> Save

Слайд 29





Example 1. Configuration File
<?xml version="1.0" encoding="UTF-8"?>
<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-3.0.xsd">
     <bean id="helloWorldService" 	class="com.bionic.edu.HelloWorldService" />
     
</beans>
Описание слайда:
Example 1. Configuration File <?xml version="1.0" encoding="UTF-8"?> <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-3.0.xsd"> <bean id="helloWorldService" class="com.bionic.edu.HelloWorldService" /> </beans>

Слайд 30





Example 1. Hello World in Spring
Create Spring project
Tune project for logging
Create project classes (POJOs)
Create Spring metadata
Create application context
Run application
Описание слайда:
Example 1. Hello World in Spring Create Spring project Tune project for logging Create project classes (POJOs) Create Spring metadata Create application context Run application

Слайд 31





Application Context 
You can load the Spring application context using the following code:
ApplicationContext ctx = new 		ClassPathXmlApplicationContext("beans.xml");
 GreetingService service = 	(GreetingService)ctx.getBean("helloWorldService");
 service.sendGreeting();
Описание слайда:
Application Context You can load the Spring application context using the following code: ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); GreetingService service = (GreetingService)ctx.getBean("helloWorldService"); service.sendGreeting();

Слайд 32





Example 1. Application Class
package com.bionic.edu;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Application {
     public static void main(String[] args) {
         ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
         GreetingService service = 
                  (GreetingService)ctx.getBean("helloWorldService");
         service.sendGreeting();
     }
}
Описание слайда:
Example 1. Application Class package com.bionic.edu; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Application { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); GreetingService service = (GreetingService)ctx.getBean("helloWorldService"); service.sendGreeting(); } }

Слайд 33





Example 1. Hello World in Spring
Create Spring project
Tune project for logging
Create project classes (POJOs)
Create Spring metadata
Create application context
Run application
Описание слайда:
Example 1. Hello World in Spring Create Spring project Tune project for logging Create project classes (POJOs) Create Spring metadata Create application context Run application

Слайд 34





Example 1.
Run application. You will get
		Hello, world!
See P211BeanDefinition project for the full text
Описание слайда:
Example 1. Run application. You will get Hello, world! See P211BeanDefinition project for the full text

Слайд 35





Injecting through Constructors
A class can be constructed in two different ways:
Using the default constructor
Using a constructor that takes an argument(s)
If no <constructor-arg> are given in a bean definition, the default constructor is used. 
A <constructor-arg> with a value attribute leads to the other constructor will be used instead.
Описание слайда:
Injecting through Constructors A class can be constructed in two different ways: Using the default constructor Using a constructor that takes an argument(s) If no <constructor-arg> are given in a bean definition, the default constructor is used. A <constructor-arg> with a value attribute leads to the other constructor will be used instead.

Слайд 36





Example 2. HelloWorldService 
package com.bionic.edu;
public class HelloWorldService implements GreetingService {
     public String message;
     public HelloWorldService(){ message = ""; }
     public HelloWorldService(String message){
	this.message = message;
     }
     public void sendGreeting() {
	System.out.println("Hello, world! " + message);
     }
}
Описание слайда:
Example 2. HelloWorldService package com.bionic.edu; public class HelloWorldService implements GreetingService { public String message; public HelloWorldService(){ message = ""; } public HelloWorldService(String message){ this.message = message; } public void sendGreeting() { System.out.println("Hello, world! " + message); } }

Слайд 37





Example 2. Bean Definition 
<bean id="helloWorldService"     	class="com.bionic.edu.HelloWorldService">
    <constructor-arg value="I am Victor." />
 </bean>
Описание слайда:
Example 2. Bean Definition <bean id="helloWorldService" class="com.bionic.edu.HelloWorldService"> <constructor-arg value="I am Victor." /> </bean>

Слайд 38





Example 2.
Run application. You will get
		Hello, world! I am Victor.
See P212ConstructorInjection project for the full text
Описание слайда:
Example 2. Run application. You will get Hello, world! I am Victor. See P212ConstructorInjection project for the full text

Слайд 39





Injecting Object References
You should use ref attribute in a <constructor-arg> for passing references to other beans
Описание слайда:
Injecting Object References You should use ref attribute in a <constructor-arg> for passing references to other beans

Слайд 40





Example 3. Application Class
public class Application {
     GreetingService greeting = null;
     public Application(){}
     public Application(GreetingService greeting){ this.greeting = greeting; }
     public static void main(String[] args) {
	ApplicationContext ctx = new 	   
                     ClassPathXmlApplicationContext("beans.xml");
	Application application = (Application)ctx.getBean("application");
	application.start();
      }
      public void start(){
	if (greeting != null) greeting.sendGreeting();
     } }
Описание слайда:
Example 3. Application Class public class Application { GreetingService greeting = null; public Application(){} public Application(GreetingService greeting){ this.greeting = greeting; } public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); Application application = (Application)ctx.getBean("application"); application.start(); } public void start(){ if (greeting != null) greeting.sendGreeting(); } }

Слайд 41





Example 3. Bean Definition 
<bean id="helloWorldService" 			
		class="com.bionic.edu.HelloWorldService">
       <constructor-arg value="I am Victor." />
</bean>
<bean name="application" class="com.bionic.edu.Application">
       <constructor-arg ref="helloWorldService" />
</bean>
Описание слайда:
Example 3. Bean Definition <bean id="helloWorldService" class="com.bionic.edu.HelloWorldService"> <constructor-arg value="I am Victor." /> </bean> <bean name="application" class="com.bionic.edu.Application"> <constructor-arg ref="helloWorldService" /> </bean>

Слайд 42





Example 3 Output
Hello, world! I am Victor.
Описание слайда:
Example 3 Output Hello, world! I am Victor.

Слайд 43





Example 3. Bean Definition 
<bean id="helloWorldService" 			
		class="com.bionic.edu.HelloWorldService">
       <constructor-arg value="I am Victor." />
</bean>
<bean id="helloKittyService" 	    
	          class="com.bionic.edu.HelloKittyService" />
<bean name="application" class="com.bionic.edu.Application">
       <constructor-arg ref="helloKittyService" />
</bean>
Описание слайда:
Example 3. Bean Definition <bean id="helloWorldService" class="com.bionic.edu.HelloWorldService"> <constructor-arg value="I am Victor." /> </bean> <bean id="helloKittyService" class="com.bionic.edu.HelloKittyService" /> <bean name="application" class="com.bionic.edu.Application"> <constructor-arg ref="helloKittyService" /> </bean>

Слайд 44





Example 3 Output
Hello, Kitty!
See P213ConstructorInjection project for the full text
Описание слайда:
Example 3 Output Hello, Kitty! See P213ConstructorInjection project for the full text

Слайд 45





Property Tags
You can use <property> tag to pass the values of different variables used at the time of object creation
<property> is similar to <constructor-arg> in many ways, except that instead of injecting values through a constructor argument, <property>injects by calling a property’s setter method.
Описание слайда:
Property Tags You can use <property> tag to pass the values of different variables used at the time of object creation <property> is similar to <constructor-arg> in many ways, except that instead of injecting values through a constructor argument, <property>injects by calling a property’s setter method.

Слайд 46





Example 4. Simple Value Injection
Add accessor to HelloWorldService class
Change beans.xml as follows
<bean id="helloWorldService" class="com.bionic.edu.HelloWorldService">
    <property name="message" value="I am Victor." />
</bean>
<bean id="helloKittyService" class="com.bionic.edu.HelloKittyService" />
<bean name="application" class="com.bionic.edu.Application">
         <constructor-arg ref="helloWorldService" />
</bean>
Описание слайда:
Example 4. Simple Value Injection Add accessor to HelloWorldService class Change beans.xml as follows <bean id="helloWorldService" class="com.bionic.edu.HelloWorldService"> <property name="message" value="I am Victor." /> </bean> <bean id="helloKittyService" class="com.bionic.edu.HelloKittyService" /> <bean name="application" class="com.bionic.edu.Application"> <constructor-arg ref="helloWorldService" /> </bean>

Слайд 47





Example 4. Output
Hello, world! I am Victor.
See P214PropertySimple project for the full text
Описание слайда:
Example 4. Output Hello, world! I am Victor. See P214PropertySimple project for the full text

Слайд 48





Simple Value Injection
<property> isn’t limited to injecting String values
The value attribute can also specify numeric (int, float, java.lang.Double, and so on) values as well as boolean values
Описание слайда:
Simple Value Injection <property> isn’t limited to injecting String values The value attribute can also specify numeric (int, float, java.lang.Double, and so on) values as well as boolean values

Слайд 49





Example 5. Numeric Injection 
public class HelloWorldService implements GreetingService {
     public String message;
     public int repeat;
    // constructors, getters&setters
     public void sendGreeting() {
          for (int i = 0; i < repeat; i++){
	     System.out.println("Hello, world! " + message);
          }
     }}
Описание слайда:
Example 5. Numeric Injection public class HelloWorldService implements GreetingService { public String message; public int repeat; // constructors, getters&setters public void sendGreeting() { for (int i = 0; i < repeat; i++){ System.out.println("Hello, world! " + message); } }}

Слайд 50





Example 5. beans.xml
<bean id="helloWorldService" class="com.bionic.edu.HelloWorldService">
      <property name="message" value="I am Victor." />
      <property name="repeat" value="3" />
</bean>
<bean id="helloKittyService" class="com.bionic.edu.HelloKittyService" />
<bean name="application" class="com.bionic.edu.Application">
       <constructor-arg ref="helloWorldService" />
</bean>
Описание слайда:
Example 5. beans.xml <bean id="helloWorldService" class="com.bionic.edu.HelloWorldService"> <property name="message" value="I am Victor." /> <property name="repeat" value="3" /> </bean> <bean id="helloKittyService" class="com.bionic.edu.HelloKittyService" /> <bean name="application" class="com.bionic.edu.Application"> <constructor-arg ref="helloWorldService" /> </bean>

Слайд 51





Example 5. Output
Hello, world! I am Victor.
Hello, world! I am Victor.
Hello, world! I am Victor.
See P215NumericInjection project for the full text
Описание слайда:
Example 5. Output Hello, world! I am Victor. Hello, world! I am Victor. Hello, world! I am Victor. See P215NumericInjection project for the full text

Слайд 52





Object Injection
The real value of DI is found in wiring an application’s collaborating objects together so that they don’t have to wire themselves together
Use ref attribute of <property> tag for this purpose
Описание слайда:
Object Injection The real value of DI is found in wiring an application’s collaborating objects together so that they don’t have to wire themselves together Use ref attribute of <property> tag for this purpose

Слайд 53





Example 5. Reference Injection
Add getter&setter for greeting field of Application class
Change beans.xml as follows
<bean id="helloWorldService" class="com.bionic.edu.HelloWorldService">
      <property name="message" value="I am Victor." />
</bean>
<bean id="helloKittyService" class="com.bionic.edu.HelloKittyService" />
<bean name="application" class="com.bionic.edu.Application">
       <property name="greeting" ref="helloKittyService" />
</bean>
Описание слайда:
Example 5. Reference Injection Add getter&setter for greeting field of Application class Change beans.xml as follows <bean id="helloWorldService" class="com.bionic.edu.HelloWorldService"> <property name="message" value="I am Victor." /> </bean> <bean id="helloKittyService" class="com.bionic.edu.HelloKittyService" /> <bean name="application" class="com.bionic.edu.Application"> <property name="greeting" ref="helloKittyService" /> </bean>

Слайд 54





Example 5. Output
Hello, Kitty!
Описание слайда:
Example 5. Output Hello, Kitty!

Слайд 55





Example 5. Bean.xml Changes
<bean id="helloWorldService" class="com.bionic.edu.HelloWorldService">
      <property name="message" value="I am Victor." />
</bean>
<bean id="helloKittyService" class="com.bionic.edu.HelloKittyService" />
<bean name="application" class="com.bionic.edu.Application">
       <property name="greeting" ref="helloWorldService" />
</bean>
Описание слайда:
Example 5. Bean.xml Changes <bean id="helloWorldService" class="com.bionic.edu.HelloWorldService"> <property name="message" value="I am Victor." /> </bean> <bean id="helloKittyService" class="com.bionic.edu.HelloKittyService" /> <bean name="application" class="com.bionic.edu.Application"> <property name="greeting" ref="helloWorldService" /> </bean>

Слайд 56





Example 5. Output
Hello, world! I am Victor.
See P216PropertyRef project for the full text
Описание слайда:
Example 5. Output Hello, world! I am Victor. See P216PropertyRef project for the full text

Слайд 57





Auto Wiring
In large applications, the number of beans will increase and the corresponding XML written to configure the numerous beans will become very large
Spring provides a feature called 'Auto-Wiring' that minimizes the XML to be written provided that certain assumptions are made about the nomenclature of beans and properties
Spring provides auto-wiring based on both XML and Annotations
Описание слайда:
Auto Wiring In large applications, the number of beans will increase and the corresponding XML written to configure the numerous beans will become very large Spring provides a feature called 'Auto-Wiring' that minimizes the XML to be written provided that certain assumptions are made about the nomenclature of beans and properties Spring provides auto-wiring based on both XML and Annotations

Слайд 58





Auto-Wiring based on Annotations
Use the <context:component-scan> tag in spring-context.xml 
Use the @Inject annotation to qualify either the member or a corresponding method (usually the setter method) which takes the injected type as argument
Описание слайда:
Auto-Wiring based on Annotations Use the <context:component-scan> tag in spring-context.xml Use the @Inject annotation to qualify either the member or a corresponding method (usually the setter method) which takes the injected type as argument

Слайд 59





The @Inject annotation 
The @Inject annotation can be used to qualify: 
a member 
any method (including setter method) which takes the injected type as argument
Описание слайда:
The @Inject annotation The @Inject annotation can be used to qualify: a member any method (including setter method) which takes the injected type as argument

Слайд 60





Autodiscovery
By default, <context:component-scan>looks for classes that are annotated as:
@Component - indicates that the class is a Spring component
@Controller - indicates that the class defines a Spring MVC controller
@Repository - the class defines a data repository
@Service - the class defines a service
Any custom annotation that is itself annotated with @Component
Описание слайда:
Autodiscovery By default, <context:component-scan>looks for classes that are annotated as: @Component - indicates that the class is a Spring component @Controller - indicates that the class defines a Spring MVC controller @Repository - the class defines a data repository @Service - the class defines a service Any custom annotation that is itself annotated with @Component

Слайд 61





@Component vs @Named
@Named and @Component annotations are used enabling a class to be auto detected as the bean definition for spring’s application context
@Named is part of the Java specification JSR-330. It is more recommended since this annotation is not tied to Spring APIs. 
@Component is part of the Spring’s annotations library.
Описание слайда:
@Component vs @Named @Named and @Component annotations are used enabling a class to be auto detected as the bean definition for spring’s application context @Named is part of the Java specification JSR-330. It is more recommended since this annotation is not tied to Spring APIs. @Component is part of the Spring’s annotations library.

Слайд 62





Example 6. Annotations
Create Spring project with name P221FirstInject
Tune pom.xml file
Tune beans.xnm file
Create application classes
Run application
Описание слайда:
Example 6. Annotations Create Spring project with name P221FirstInject Tune pom.xml file Tune beans.xnm file Create application classes Run application

Слайд 63





Example 6. pom.xml 
Add the following dependency to the project’s pom.xml file:
<dependency>
	<groupId>javax.inject</groupId>
	<artifactId>javax.inject</artifactId>
	<version>1</version>
</dependency>
Описание слайда:
Example 6. pom.xml Add the following dependency to the project’s pom.xml file: <dependency> <groupId>javax.inject</groupId> <artifactId>javax.inject</artifactId> <version>1</version> </dependency>

Слайд 64





Example 6. beans.xml
Create the following beans.xml file:
<?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-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <context:component-scan base-package="com.bionic.edu" />
</beans>
Описание слайда:
Example 6. beans.xml Create the following beans.xml file: <?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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="com.bionic.edu" /> </beans>

Слайд 65





Example 6. HelloWorldService
package com.bionic.edu;
import javax.inject.Named;
@Named
public class HelloWorldService implements GreetingService {
    public String message;
    public int repeat;
    .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 
    public void sendGreeting() {
        for (int i = 0; i < repeat; i++){
	System.out.println("Hello, world! " + message);
        }
    }
}
Описание слайда:
Example 6. HelloWorldService package com.bionic.edu; import javax.inject.Named; @Named public class HelloWorldService implements GreetingService { public String message; public int repeat; . . . . . . . . . . . . . . . . public void sendGreeting() { for (int i = 0; i < repeat; i++){ System.out.println("Hello, world! " + message); } } }

Слайд 66





Example 6. Application Class
@Named
public class Application {
    @Inject
    GreetingService greeting = null;
    .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 
    public static void main(String[] args) {
        ApplicationContext ctx = new 	ClassPathXmlApplicationContext("beans.xml");
        Application application = (Application)ctx.getBean("application");
        application.start();
    }
    public void start(){
        if (greeting != null) greeting.sendGreeting();
    }}
Описание слайда:
Example 6. Application Class @Named public class Application { @Inject GreetingService greeting = null; . . . . . . . . . . . . . . . . public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); Application application = (Application)ctx.getBean("application"); application.start(); } public void start(){ if (greeting != null) greeting.sendGreeting(); }}

Слайд 67





Example 6. Output
Hello, world! 
See P221FirstInject project for the full text
Описание слайда:
Example 6. Output Hello, world! See P221FirstInject project for the full text

Слайд 68





Example 7. Autowiring by Name
Annotate HelloKittyService class with @Named:
package com.bionic.edu;
import javax.inject.Named;
@Named
public class HelloKittyService implements GreetingService {
    public void sendGreeting(){
	System.out.println("Hello, Kitty!");
    }
}
Описание слайда:
Example 7. Autowiring by Name Annotate HelloKittyService class with @Named: package com.bionic.edu; import javax.inject.Named; @Named public class HelloKittyService implements GreetingService { public void sendGreeting(){ System.out.println("Hello, Kitty!"); } }

Слайд 69





Example 7. Output
Running the application leads to an exception
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'application': Injection of autowired dependencies failed; 
   .  .  .  .  .  .  .  .  .
No unique bean of type [com.bionic.edu.GreetingService] is defined: 
expected single matching bean but found 2: [helloKittyService, helloWorldService]
Описание слайда:
Example 7. Output Running the application leads to an exception Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'application': Injection of autowired dependencies failed; . . . . . . . . . No unique bean of type [com.bionic.edu.GreetingService] is defined: expected single matching bean but found 2: [helloKittyService, helloWorldService]

Слайд 70





The @Named Annotation 
If multiple bean types are available for injection, then Spring will be unable to make a decision on which bean to inject and will throw an Exception
In such cases, we can use the @Named(name="..") annotation and give the name of the bean that we want Spring to inject.
Описание слайда:
The @Named Annotation If multiple bean types are available for injection, then Spring will be unable to make a decision on which bean to inject and will throw an Exception In such cases, we can use the @Named(name="..") annotation and give the name of the bean that we want Spring to inject.

Слайд 71





Example 7. @Named Annotation
@Named
public class Application {
    @Inject
    @Named("helloWorldService")
    GreetingService greeting = null;
    .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 
    public static void main(String[] args) {
        ApplicationContext ctx = new 	ClassPathXmlApplicationContext("beans.xml");
        Application application = (Application)ctx.getBean("application");
        application.start();
    }
 .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Описание слайда:
Example 7. @Named Annotation @Named public class Application { @Inject @Named("helloWorldService") GreetingService greeting = null; . . . . . . . . . . . . . . . . public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); Application application = (Application)ctx.getBean("application"); application.start(); } . . . . . . . . . . . . . . . .

Слайд 72





Example 7. Output
Hello, world!
Описание слайда:
Example 7. Output Hello, world!

Слайд 73





Example 7. @Named Annotation
@Named
public class Application {
    @Inject
    @Named("helloKittyService")
    GreetingService greeting = null;
    .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 
    public static void main(String[] args) {
        ApplicationContext ctx = new 	ClassPathXmlApplicationContext("beans.xml");
        Application application = (Application)ctx.getBean("application");
        application.start();
    }
 .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
Описание слайда:
Example 7. @Named Annotation @Named public class Application { @Inject @Named("helloKittyService") GreetingService greeting = null; . . . . . . . . . . . . . . . . public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); Application application = (Application)ctx.getBean("application"); application.start(); } . . . . . . . . . . . . . . . .

Слайд 74





Example 7. Output
Hello, Kitty!
See P222InjectByName project for the full text
Описание слайда:
Example 7. Output Hello, Kitty! See P222InjectByName project for the full text



Похожие презентации
Mypresentation.ru
Загрузить презентацию