企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
Bean 的定义 Property Explained in…​ class Instantiating beans name Naming beans scope Bean scopes constructor arguments Dependency Injection properties Dependency Injection autowiring mode Autowiring collaborators lazy-initialization mode Lazy-initialized beans initialization method Initialization callbacks destruction method Destruction callbacks 也可以定义bean之后再加入容器。 registerSingleton(..) and registerBeanDefinition(..). 可以通过 id 和name给bean命名。 component scanning的方式, Spring会给没有命名的Component自动命名, 基本上是采用简单的类名并将其初始字符转换为小写 其他命名: <alias name="fromName" alias="toName"/> 一般状况, 是空的构造函数就可以。 有静态方法 <bean id="clientService" class="examples.ClientService" factory-method="createInstance"/> public class ClientService { private static ClientService clientService = new ClientService(); private ClientService() {} public static ClientService createInstance() { return clientService; } } } 使用实例工厂化方法 <bean id="serviceLocator" class="examples.DefaultServiceLocator"> <!-- inject any dependencies required by this locator bean --> </bean> <!-- the bean to be created via the factory bean --> <bean id="clientService" factory-bean="serviceLocator" factory-method="createClientServiceInstance"/> public class DefaultServiceLocator { private static ClientService clientService = new ClientServiceImpl(); public ClientService createClientServiceInstance() { return clientService; } } }