Spring Auto scanning components

此页面是否是列表页或首页?未找到合适正文内容。

Spring Auto scanning components

标签:bspcommretentionracbetweenoutputashctosch

Normally you declare all the beans or components in XML bean configuration file, so that Spring container can detect and register your beans or components. Actually, Spring is able to auto scan, detect and instantiate your beans from pre-defined project package, no more tedious beans declaration in in XML file.

Following is a simple Spring project, including a customer service and dao layer. Let’s explore the different between declare components manually and auto components scanning in Spring.

1. Declares Components Manually

See a normal way to declare a bean in Spring.

Normal bean.

package com.mkyong.customer.dao;

public class CustomerDAO
{
@Override
public String toString() {
return \”Hello , This is CustomerDAO\”;
}
}

DAO layer.

package com.mkyong.customer.services;

import com.mkyong.customer.dao.CustomerDAO;

public class CustomerService
{
CustomerDAO customerDAO;

public void setCustomerDAO(CustomerDAO customerDAO) {
this.customerDAO = customerDAO;
}

@Override
public String toString() {
return \”CustomerService [customerDAO=\” + customerDAO + \”]\”;
}

}

Bean configuration file (Spring-Customer.xml), a normal bean configuration in Spring.

<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-2.5.xsd\”

作者: liuzhihao

为您推荐

返回顶部