VietTuts

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

  • Home
  • Java
  • Servlet
  • JSP
  • Struts2
  • Hibernate
  • Spring
  • MyBatis
  • Java WS
  • C
  • C++
  • C#
  • Python
  • PHP
  • Excel
  • VBA
  • Web
    • JavaScript
    • JQUERY
    • JSON
    • AJAX
    • CSS
    • HTML
    • HTML5
    • Node.js
    • Angular 7
  • SQL
    • MySQL
    • SQL Server
  • Misc
    • Eclipse
    • Phần mềm tiện ích
    • Cấu trúc DL&GT
    • 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
1 / 3
❮ ❯

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 SpringSắp Tết 2026 Rồi! - Còn bao nhiêu ngày nữa là đến tết 2026?

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 | Hibernate | Spring
Học Excel | Excel VBA
Học Servlet | JSP | Struts2
Học C | C++ | C#
Học Python
Học SQL

Bài Tập Có Lời Giải

Bài tập Java
Bài tập C
Bài tập C++
Bài tập C#
Bài tập Python
Ví dụ Excel VBA

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

Scroll back to top

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