VietTuts

Tự Học Lập Trình Online

  • Home
  • Java
  • Servlet
  • JSP
  • Struts2
  • Hibernate
  • Spring
  • MyBatis
  • Java WS
  • C
  • C++
  • Python
  • PHP
  • Eclipse
  • VBA
  • Web
    • JavaScript
    • JQUERY
    • JSON
    • AJAX
    • CSS
    • HTML
    • Node.js
    • Angular 7
  • SQL
    • MySQL
    • SQL Server
  • Misc
    • Phần mềm tiện ích
    • Cấu trúc dữ liệu và giải thuật
    • Học lập trình C#
    • Selenium Test

Spring Tuts

Spring là gì? Kiến trúc Spring Spring - Thiết lập môi trường Spring HelloWorld Example Dependency Injection trong Spring IoC Containers trong Spring Định nghĩa Bean trong Spring Phạm vi của Bean trong Spring Vòng đời của Bean trong Spring Spring - Kế thừ Bean Spring - Inner Bean Spring - Injecting Collection Spring - Beans Auto-Wiring Annotation Based Configuration Spring - Java Based Configuration Spring - Event Handling in Spring Spring - Custom Events in Spring Spring - AOP with Spring Framework Spring - JDBC Framework Spring - Transaction Management Spring - Web MVC Framework Spring - Logging with Log4J

Spring Tool

Cài đặt Spring Tool Suite (STS) trong Eclipse Tạo Spring project bằng Spring Tool Suite trong Eclipse Tạo Spring project (annotation) bằng Spring Tool Suite (STS)

Spring 4 Example

Ví dụ login trong Spring 4 Web MVC – Hibernate 4 XML Mapping Ví dụ login trong Spring 4 Web MVC – Hibernate 4 Annotation

Inner Bean trong Spring


Previous
Next

Như bạn biết, các lớp bên trong Java được định nghĩa trong phạm vi của các lớp khác, tương tự, các inner Bean trong Spring là các bean được định nghĩa trong phạm vi của một bean khác. Do đó, một phần tử <bean/> bên trong <property/> hoặc <constructor-arg/> được gọi là inner bean, cho ví dụ:

<?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 = "outerBean" class = "...">
      <property name = "target">
         <bean id = "innerBean" class = "..."/>
      </property>
   </bean>

</beans>

Nội dung chính

  • Ví dụ Inner Bean trong Spring
  • Download Project

Ví dụ Inner Bean trong Spring

Tạo một Java Maven project trong Eclipse.

Cấu trúc project:

Inner Bean trong Spring

Sau đó update file pom.xml để tải thư viện Spring như sau:

<project xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
  http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>vn.viettuts</groupId>
  <artifactId>SpringExample</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>SpringExample</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.3.6.RELEASE</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.3.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.6.RELEASE</version>
        </dependency>
  </dependencies>
</project>

Đây là nội dung của lớp TextEditor.java

package vn.viettuts.example;

public class TextEditor {
    private SpellChecker spellChecker;

    // inject dependency qua tham số của phương thức.
    public void setSpellChecker(SpellChecker spellChecker) {
        System.out.println("Set bo kiem tra chinh ta.");
        this.spellChecker = spellChecker;
    }

    public SpellChecker getSpellChecker() {
        return spellChecker;
    }

    public void spellCheck() {
        spellChecker.checkSpelling();
    }
}

Đây là nội dung của lớp SpellChecker.java

package vn.viettuts.example;

public class SpellChecker {
    public SpellChecker() {
        System.out.println("Khoi bo kiem tra chinh ta.");
    }

    public void checkSpelling() {
        System.out.println("Kiem tra chinh ta...");
    }
}

Đây là nội dung của lớp TextEditor.java

package vn.viettuts.example;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Hello world!
 *
 */
public class MainApp {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                "Beans.xml");
        TextEditor te = (TextEditor) context.getBean("textEditor");
        te.spellCheck();
    }
}

Sau đây là tệp cấu hình Beans.xml có cấu hình cho phép dựa trên setter nhưng sử dụng inner Bean.

<?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">

    <!-- Definition for textEditor bean using inner bean -->
    <bean id="textEditor" class="vn.viettuts.example.TextEditor">
        <property name="spellChecker">
            <bean id="spellChecker" class="vn.viettuts.example.SpellChecker" />
        </property>
    </bean>

</beans>

Kết quả:

Khoi doi tuong kiem tra chinh ta.
Set bo kiem tra chinh ta.
Kien tra chinh ta...

Ngoài ra, bạn cũng có thể dùng thuộc tính ref để tham chiến inner bean tới một bean khác như sau, cũng tương tự như định nghĩa trên:

<?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="SpellChecker" class="vn.viettuts.example.SpellChecker" />
    
    <bean id="textEditor" class="vn.viettuts.example.TextEditor">
        <property name="spellChecker" ref="SpellChecker" />
    </bean>

</beans>


Download Project

Download Now!

Previous
Next

Recent Updates

SpringLayout trong Java SwingMyBatis Example - MyBatis + SpringSpring là gì?Cài đặt Spring Tool Suite (STS) trong EclipseDependency Injection trong SpringĐịnh nghĩa Bean trong SpringInjecting Collection trong SpringInner Bean trong SpringIoC Container trong SpringKế thừa Bean trong SpringKiến trúc SpringPhạm vi của Bean trong Spring

VietTuts on facebook

Học Lập Trình Online Miễn Phí - VietTuts.Vn

Danh sách bài học

Học java
Học servlet
Học jsp
Học Hibernate
Học Struts2
Học Spring
Học SQL

Câu hỏi phỏng vấn

201 câu hỏi phỏng vấn java
25 câu hỏi phỏng vấn servlet
75 câu hỏi phỏng vấn jsp
52 câu hỏi phỏng vấn Hibernate
70 câu hỏi phỏng vấn Spring
57 câu hỏi phỏng vấn SQL

About VietTuts.Vn

Hệ thống bài học trên VietTuts.Vn bao gồm các bài lý thuyết và thực hành về các công nghệ java và công nghệ web. Các bài lý thuyết trên hệ thống VietTuts.Vn được tham khảo và tổng hợp từ các trang http://javatpoint.com, http://www.tutorialspoint.com, http://docs.oracle.com/en …

Scroll back to top

Copyright © 2016 VietTuts.Vn all rights reserved. | VietTuts.Vn team | Liên hệ | Chính sách - riêng tư | sitemap.html | sitemap_index.xml