Презентация 2. Java Spring Core 3. Spring IoC Container онлайн
На нашем сайте вы можете скачать и просмотреть онлайн доклад-презентацию на тему 2. Java Spring Core 3. Spring IoC Container абсолютно бесплатно. Урок-презентация на эту тему содержит всего 74 слайда. Все материалы созданы в программе PowerPoint и имеют формат ppt или же pptx. Материалы и темы для презентаций взяты из открытых источников и загружены их авторами, за качество и достоверность информации в них администрация сайта не отвечает, все права принадлежат их создателям. Если вы нашли то, что искали, отблагодарите авторов - поделитесь ссылкой в социальных сетях, а наш сайт добавьте в закладки.
Презентации » Устройства и комплектующие » 2. Java Spring Core 3. Spring IoC Container
Оцените!
Оцените презентацию от 1 до 5 баллов!
- Тип файла:ppt / pptx (powerpoint)
- Всего слайдов:74 слайда
- Для класса:1,2,3,4,5,6,7,8,9,10,11
- Размер файла:0.96 MB
- Просмотров:75
- Скачиваний:0
- Автор:неизвестен
Слайды и текст к этой презентации:
№3 слайд
![Dependency Injection The](/documents_6/29cdfb4c1f50a70b70e8ae89c8d6dfca/img2.jpg)
Содержание слайда: 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.
№7 слайд
![Spring Bean Definition The](/documents_6/29cdfb4c1f50a70b70e8ae89c8d6dfca/img6.jpg)
Содержание слайда: 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 lt ?xml version](/documents_6/29cdfb4c1f50a70b70e8ae89c8d6dfca/img7.jpg)
Содержание слайда: 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>
№10 слайд
![Bean Definition Attributes](/documents_6/29cdfb4c1f50a70b70e8ae89c8d6dfca/img9.jpg)
Содержание слайда: 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
№16 слайд
![Add Dependencies to the](/documents_6/29cdfb4c1f50a70b70e8ae89c8d6dfca/img15.jpg)
Содержание слайда: 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](/documents_6/29cdfb4c1f50a70b70e8ae89c8d6dfca/img16.jpg)
Содержание слайда: 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
№20 слайд
![Example . log j .xml in](/documents_6/29cdfb4c1f50a70b70e8ae89c8d6dfca/img19.jpg)
Содержание слайда: 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>
№26 слайд
![Bean Definition The lt bean](/documents_6/29cdfb4c1f50a70b70e8ae89c8d6dfca/img25.jpg)
Содержание слайда: 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.
№29 слайд
![Example . Configuration File](/documents_6/29cdfb4c1f50a70b70e8ae89c8d6dfca/img28.jpg)
Содержание слайда: 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>
№32 слайд
![Example . Application Class](/documents_6/29cdfb4c1f50a70b70e8ae89c8d6dfca/img31.jpg)
Содержание слайда: 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();
}
}
№35 слайд
![Injecting through](/documents_6/29cdfb4c1f50a70b70e8ae89c8d6dfca/img34.jpg)
Содержание слайда: 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 . HelloWorldService](/documents_6/29cdfb4c1f50a70b70e8ae89c8d6dfca/img35.jpg)
Содержание слайда: 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);
}
}
№40 слайд
![Example . Application Class](/documents_6/29cdfb4c1f50a70b70e8ae89c8d6dfca/img39.jpg)
Содержание слайда: 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();
} }
№43 слайд
![Example . Bean Definition lt](/documents_6/29cdfb4c1f50a70b70e8ae89c8d6dfca/img42.jpg)
Содержание слайда: 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>
№45 слайд
![Property Tags You can use lt](/documents_6/29cdfb4c1f50a70b70e8ae89c8d6dfca/img44.jpg)
Содержание слайда: 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 . Simple Value](/documents_6/29cdfb4c1f50a70b70e8ae89c8d6dfca/img45.jpg)
Содержание слайда: 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>
№49 слайд
![Example . Numeric Injection](/documents_6/29cdfb4c1f50a70b70e8ae89c8d6dfca/img48.jpg)
Содержание слайда: 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 . beans.xml lt bean](/documents_6/29cdfb4c1f50a70b70e8ae89c8d6dfca/img49.jpg)
Содержание слайда: 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>
№53 слайд
![Example . Reference Injection](/documents_6/29cdfb4c1f50a70b70e8ae89c8d6dfca/img52.jpg)
Содержание слайда: 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>
№55 слайд
![Example . Bean.xml Changes lt](/documents_6/29cdfb4c1f50a70b70e8ae89c8d6dfca/img54.jpg)
Содержание слайда: 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>
№57 слайд
![Auto Wiring In large](/documents_6/29cdfb4c1f50a70b70e8ae89c8d6dfca/img56.jpg)
Содержание слайда: 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
№60 слайд
![Autodiscovery By default, lt](/documents_6/29cdfb4c1f50a70b70e8ae89c8d6dfca/img59.jpg)
Содержание слайда: 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](/documents_6/29cdfb4c1f50a70b70e8ae89c8d6dfca/img60.jpg)
Содержание слайда: @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.
№64 слайд
![Example . beans.xml Create](/documents_6/29cdfb4c1f50a70b70e8ae89c8d6dfca/img63.jpg)
Содержание слайда: 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 . HelloWorldService](/documents_6/29cdfb4c1f50a70b70e8ae89c8d6dfca/img64.jpg)
Содержание слайда: 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 . Application Class](/documents_6/29cdfb4c1f50a70b70e8ae89c8d6dfca/img65.jpg)
Содержание слайда: 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();
}}
№69 слайд
![Example . Output Running the](/documents_6/29cdfb4c1f50a70b70e8ae89c8d6dfca/img68.jpg)
Содержание слайда: 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](/documents_6/29cdfb4c1f50a70b70e8ae89c8d6dfca/img69.jpg)
Содержание слайда: 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 . Named Annotation](/documents_6/29cdfb4c1f50a70b70e8ae89c8d6dfca/img70.jpg)
Содержание слайда: 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();
}
. . . . . . . . . . . . . . . .
№73 слайд
![Example . Named Annotation](/documents_6/29cdfb4c1f50a70b70e8ae89c8d6dfca/img72.jpg)
Содержание слайда: 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();
}
. . . . . . . . . . . . . . . .
Скачать все slide презентации 2. Java Spring Core 3. Spring IoC Container одним архивом:
Похожие презентации
-
2. Java Spring Core 2. Maven
-
2. Java Spring Core 1. Overview
-
Spring Core. Spring AOP
-
Создание приложения, используя Spring Framework. Java. (Лекция 23)
-
Микросервисы - buzzword или спасение? HiLoad и как это сделать на Java/Spring
-
Multithreading IO Streams Java Core
-
String Regular Expression. Java Core
-
Generic Collections Java Core
-
Arrays Loops. Java Core
-
Conditional statements. Java Core