context的component-scan
component.xml
<?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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.component.*"></context:component-scan>
</beans>
Login.java
package com.component.entity;
import org.springframework.stereotype.Component;
@Component("entity")// 如果注解中没有值,getbean中name为类名首字母小写,有值name则为参数值
public class Login {
String account;
String password;
public Login(){
System.out.println("@Component...");
}
public Login(String account, String password){
this.account=account;
this.password=password;
System.out.println("@Component param...");
}
}
Test.java
package com.component;
import com.component.controller.LoginController;
import com.component.dao.LoginDAO;
import com.component.dao.LoginDAOImpl;
import com.component.entity.Login;
import com.component.service.LoginService;
import com.component.service.LoginServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("component.xml");
Login login = applicationContext.getBean("entity", Login.class);
LoginController loginController = applicationContext.getBean("controllerId", LoginController.class);
LoginDAO loginDAO = applicationContext.getBean("repositoryId", LoginDAOImpl.class);
LoginService loginService = applicationContext.getBean("serviceId", LoginServiceImpl.class);
System.out.println(login);
System.out.println(loginController);
System.out.println(loginDAO);
System.out.println(loginService);
}
}
Q:spring中,factorybean与beanFactory的区别
BeanFactory文件
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package org.springframework.beans.factory;
import org.springframework.beans.BeansException;
import org.springframework.core.ResolvableType;
public interface BeanFactory {
String FACTORY_BEAN_PREFIX = "&";
Object getBean(String var1) throws BeansException;
<T> T getBean(String var1, Class<T> var2) throws BeansException;
<T> T getBean(Class<T> var1) throws BeansException;
Object getBean(String var1, Object... var2) throws BeansException;
<T> T getBean(Class<T> var1, Object... var2) throws BeansException;
boolean containsBean(String var1);
boolean isSingleton(String var1) throws NoSuchBeanDefinitionException;
boolean isPrototype(String var1) throws NoSuchBeanDefinitionException;
boolean isTypeMatch(String var1, ResolvableType var2) throws NoSuchBeanDefinitionException;
boolean isTypeMatch(String var1, Class<?> var2) throws NoSuchBeanDefinitionException;
Class<?> getType(String var1) throws NoSuchBeanDefinitionException;
String[] getAliases(String var1);
}
是ApplicationContext和ClassPathXmlApplicationContext的父接口,有常用的getBean方法,
BeanFactory文件
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package org.springframework.beans.factory;
public interface FactoryBean<T> {
T getObject() throws Exception;
Class<?> getObjectType();
boolean isSingleton();
}
通过自定义类实现BeanFactory接口,实现
getObject
getObjectType
isSingleton
的接口方法,实现工厂bean模式注入,spring工厂bean创建对象