使用方法1:

  //在执行此实例化的时候就会完成所有注入
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); UserService service = (UserService)ctx.getBean("userService");

使用方法2:

public class SocketRequest {

/**
* 默认实例
*/
private static final SocketRequest INSTANCE = new SocketRequest();

/**
* 获取默认实例
*
* @return 默认实例
*/
public static SocketRequest get() {
return INSTANCE;
}

/**
* 处理系统控制器操作方法
*
* @param context
* spring上下文
*/
public void methodHolder(ApplicationContext context) {
String[] beans = context.getBeanDefinitionNames();//通过此方法可以获得所有的注入类

}
}

public class GameServer implements ApplicationContextAware{
@Override
public void setApplicationContext(ApplicationContext arg0)
throws BeansException {
SocketRequest.get().methodHolder(arg0);
}
}
public class mainServer {

    public static void main(String[] args) {

//在执行此实例化的时候就会完成所有注入,同时会调用GameServer的setApplicationContext方法
GameServer server = new ClassPathXmlApplicationContext("server.xml").getBean(GameServer.class);

} 
}

Spring配置

1.属性中引用另外的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-2.5.xsd">
<!-- IoC控制反转 -->
<bean id="u" class="com.bjsxt.dao.impl.UserDAOImpl">
</bean>
<!-- 属性中引用另外的bean-->
<bean id="userService" class="com.bjsxt.service.UserService">
<property name="userDAO" ref="u" />
</bean>
</beans>

2.有构造函数的注入

<?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-2.5.xsd">
<!-- IoC控制反转 -->
<bean id="u" class="com.bjsxt.dao.impl.UserDAOImpl">
</bean>
<bean id="userService" class="com.bjsxt.service.UserService">
<!-- 有构造函数的注入 -->
<constructor-arg>
<ref bean="u" />
</constructor-arg>
</bean>
</beans>

3.有属性的注入,直接把属性写入,很少用到

<?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-2.5.xsd">
<!-- IoC控制反转 -->
<bean name="u" class="com.bjsxt.dao.impl.UserDAOImpl">
<!-- 有属性的注入,直接把属性写入,很少用到 -->
<property name="daoId" value="123"></property>
<property name="daoStatus" value="DDD"></property>
</bean>
<!-- 可以写id也可以写name(如果是name则可以写特殊字符) -->
<bean id="userService" class="com.bjsxt.service.UserService">
<constructor-arg>
<ref bean="u" />
</constructor-arg>
</bean>
</beans>

4.scope范围,默认是singleton即单例,如果是prototype则每次是新实例

<?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-2.5.xsd">
<!-- IoC控制反转 -->
<bean name="u" class="com.bjsxt.dao.impl.UserDAOImpl">
<property name="daoId" value="123"></property>
<property name="daoStatus" value="DDD"></property>
</bean>
<!-- scope范围,默认是singleton即单例,如果是prototype则每次是新实例 -->
<bean id="userService" class="com.bjsxt.service.UserService" scope="prototype">
<constructor-arg>
<ref bean="u" />
</constructor-arg>
</bean>
</beans>

5.可以注入集合类型

<?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-2.5.xsd">
<!-- IoC控制反转 -->
<bean name="u" class="com.bjsxt.dao.impl.UserDAOImpl">
<!-- 可以注入集合类型 -->
<property name="sets">
<set>
<value>1</value>
<value>2</value>
</set>
</property>
<property name="lists">
<list>
<value>1</value>
<value>2</value>
<value>3</value>
</list>
</property>
<property name="maps">
<map>
<entry key="1" value="1"></entry>
<entry key="2" value="2"></entry>
<entry key="3" value="3"></entry>
<entry key="4" value="4"></entry>
</map>
</property>
</bean>
<bean id="userService" class="com.bjsxt.service.UserService"
scope="prototype">
<constructor-arg>
<ref bean="u" />
</constructor-arg>
</bean>
</beans>

6.自动装配

<?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-2.5.xsd">
<!-- IoC控制反转 -->
<bean name="userDAO" class="com.bjsxt.dao.impl.UserDAOImpl">
<property name="name" value="myname"></property>
</bean>
<!-- 自动装配(用的不多):
byName按名称自动匹配(即要装配的bean中的属性名称和配置中的bean名称相同就会自动装配,如UserService类中的属性和userDAO的bean名称相同就自动装配)
byType按类型自动匹配 (即要装配的bean中的属性类型和配置中的bean的类型相同就会自动装配,如UserService类中的属性类型和userDAO的bean类型相同就自动装配)
-->
<bean id="userService" class="com.bjsxt.service.UserService"
autowire="default">
</bean> </beans>

7.初始化bean时执行init-method方法和销毁的时候执行destroy-method方法

<?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-2.5.xsd"
default-autowire="byName" >
<!-- IoC控制反转 -->
<bean name="userDAO" class="com.bjsxt.dao.impl.UserDAOImpl">
<property name="name" value="myname"></property>
</bean>
<!-- 初始化bean时执行init-method方法和销毁的时候执行destroy-method方法 -->
<bean id="userService" class="com.bjsxt.service.UserService"
init-method="init" destroy-method="destroy">
</bean>
</beans>

8.使用注解

<?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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config></context:annotation-config>
</beans>

9.扫描包名,包名下的类都可以注入。

<?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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="com.bjsxt"></context:component-scan>
</beans>

10.实际项目配置参考

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.eelpo.com/schema/jdbc" xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.eelpo.com/schema/jdbc
http://www.eelpo.com/schema/jdbc/jdbc.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <context:property-placeholder location="classpath:system.properties" /> <context:component-scan base-package="com.egaplay.foi.controller" />
<context:component-scan base-package="com.egaplay.foi.core.listener" />
<context:component-scan base-package="com.egaplay.foi.tools.controller" />
<context:component-scan base-package="com.egaplay.foi.module.*.service" />
<context:component-scan base-package="com.egaplay.foi.module" resource-pattern="Context.class" /> <jdbc:repositories base-package="com.egaplay.foi.module.*.dao"></jdbc:repositories> <bean class="com.eelpo.framework.socket.server.GameServer">
<property name="port" value="${port}" />
<property name="shutdownPort" value="${shutdownPort}" />
<property name="crossDomainPort" value="${crossDomainPort}" />
<property name="startCrossDomainServer" value="${startCrossDomainServer}" />
<property name="shutdownCommand" value="${shutdownCommand}" />
<property name="executionHandler" ref="executionHandler" />
<property name="socketIdleStateHandler" ref="socketIdleStateHandler" />
<property name="gameServerListener" ref="foiServerListener" />
<property name="socketIdleListener" ref="foiSocketIdleListener" />
<property name="socketSessionListener" ref="foiSocketSessionListener" />
<property name="socketContextListener" ref="foiSocketContextListener" />
<property name="socketRequestListener" ref="foiSocketRequestListener" />
</bean> <bean id="executionHandler" class="org.jboss.netty.handler.execution.ExecutionHandler">
<constructor-arg index="0">
<bean class="org.jboss.netty.handler.execution.OrderedMemoryAwareThreadPoolExecutor">
<constructor-arg index="0" value="${threadPool.corePoolSize}" type="int" />
<constructor-arg index="1" value="${threadPool.maxChannelMemorySize}" type="long" />
<constructor-arg index="2" value="${threadPool.maxTotalMemorySize}" type="long" />
<constructor-arg index="3" value="${threadPool.keepAliveTime}" type="long" />
<constructor-arg index="4" value="SECONDS" type="java.util.concurrent.TimeUnit" />
<constructor-arg index="5">
<bean class="com.eelpo.framework.utils.concurrent.NamedThreadFactory">
<constructor-arg index="0" value="Business Process #" />
</bean>
</constructor-arg>
</bean>
</constructor-arg>
</bean> <bean id="socketIdleStateHandler" class="com.eelpo.framework.socket.server.handler.SocketIdleStateHandler">
<constructor-arg index="0" ref="foiSocketIdleListener" />
<constructor-arg index="1">
<bean class="org.jboss.netty.util.HashedWheelTimer">
<constructor-arg index="0" value="${wheelTimer.tickDuration}" type="long" />
<constructor-arg index="1" value="SECONDS" type="java.util.concurrent.TimeUnit" />
<constructor-arg index="2" value="${wheelTimer.ticksPerWheel}" type="int" />
</bean>
</constructor-arg>
<constructor-arg index="2" value="${socketIdle.readerIdleTimeSeconds}" type="int" />
<constructor-arg index="3" value="${socketIdle.writerIdleTimeSeconds}" type="int" />
<constructor-arg index="4" value="${socketIdle.allIdleTimeSeconds}" type="int" />
</bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="url" value="${jdbc.url}" />
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="connectionProperties" value="${jdbc.connectionProperties}" />
<property name="defaultAutoCommit" value="${dbcp.defaultAutoCommit}" />
<property name="defaultCatalog" value="${dbcp.defaultCatalog}" />
<property name="initialSize" value="${dbcp.initialSize}" />
<property name="maxActive" value="${dbcp.maxActive}" />
<property name="maxIdle" value="${dbcp.maxIdle}" />
<property name="minIdle" value="${dbcp.minIdle}" />
<property name="maxWait" value="${dbcp.maxWait}" />
<property name="timeBetweenEvictionRunsMillis" value="${dbcp.timeBetweenEvictionRunsMillis}" />
<property name="numTestsPerEvictionRun" value="${dbcp.numTestsPerEvictionRun}" />
<property name="minEvictableIdleTimeMillis" value="${dbcp.minEvictableIdleTimeMillis}" />
<property name="poolPreparedStatements" value="${dbcp.poolPreparedStatements}" />
<property name="maxOpenPreparedStatements" value="${dbcp.maxOpenPreparedStatements}" />
<property name="accessToUnderlyingConnectionAllowed" value="${dbcp.accessToUnderlyingConnectionAllowed}" />
<property name="removeAbandoned" value="${dbcp.removeAbandoned}" />
<property name="removeAbandonedTimeout" value="${dbcp.removeAbandonedTimeout}" />
<property name="logAbandoned" value="${dbcp.logAbandoned}" />
</bean>
</beans>

Spring中依赖注入的使用和配置的更多相关文章

  1. 使用IDEA详解Spring中依赖注入的类型(上)

    使用IDEA详解Spring中依赖注入的类型(上) 在Spring中实现IoC容器的方法是依赖注入,依赖注入的作用是在使用Spring框架创建对象时动态地将其所依赖的对象(例如属性值)注入Bean组件 ...

  2. Spring中依赖注入的四种方式

    在Spring容器中为一个bean配置依赖注入有三种方式: · 使用属性的setter方法注入  这是最常用的方式: · 使用构造器注入: · 使用Filed注入(用于注解方式). 使用属性的sett ...

  3. spring中依赖注入

    理解依赖注入:参考https://blog.csdn.net/taijianyu/article/details/2338311 一.依赖注入让bean与bean之间以配置文件组织在一起,而不是以硬编 ...

  4. spring中依赖注入与aop讲解

    一.依赖注入 这个属于IOC依赖注入,也叫控制反转,IOC是说类的实例由容器产生,而不是我们用new的方式创建实例,控制端发生了改变所以叫控制反转. 1 2 3 4 5 6 7 8 9 10 11 1 ...

  5. Spring点滴七:Spring中依赖注入(Dependency Injection:DI)

    Spring机制中主要有两种依赖注入:Constructor-based Dependency Injection(基于构造方法依赖注入) 和 Setter-based Dependency Inje ...

  6. Spring系列.依赖注入配置

    依赖注入的配置 Spring的依赖注入分为基于构造函数的依赖注入和基于setter方法的依赖注入. 基于构造函数的依赖注入 <!-- 通过构造器参数索引方式依赖注入 --> <bea ...

  7. Spring IOC 依赖注入的两种方式XML和注解

    依赖注入的原理 依赖注入的方式---XML配置 依赖注入的方式---注解的方式 Spring 它的核心就是IOC和AOP.而IOC中实现Bean注入的实现方式之一就是DI(依赖注入). 一 DI的原理 ...

  8. (spring-第3回【IoC基础篇】)spring的依赖注入-属性、构造函数、工厂方法等的注入(基于XML)

    Spring要把xml配置中bean的属性实例化为具体的bean,"依赖注入"是关卡.所谓的"依赖注入",就是把应用程序对bean的属性依赖都注入到spring ...

  9. Spring的依赖注入(DI)三种方式

    Spring依赖注入(DI)的三种方式,分别为: 1.  接口注入 2.  Setter方法注入 3.  构造方法注入 下面介绍一下这三种依赖注入在Spring中是怎么样实现的. 首先我们需要以下几个 ...

随机推荐

  1. 如何才能将Faster R-CNN训练起来?

    如何才能将Faster R-CNN训练起来? 首先进入 Faster RCNN 的官网啦,即:https://github.com/rbgirshick/py-faster-rcnn#installa ...

  2. jquery 自动补全控件(支持IE6)待整理

    自动补全控件(兼容IE6):http://bassistance.de/ download地址:http://jquery.bassistance.de/autocomplete/jquery.aut ...

  3. ABBYY导出结果为PDF注意事项

    使用ABBYY FineReader Pro for Mac OCR文字识别软件识别文档时,可以将已识别的文本保存到文件中,还可以通过电子邮件发送已识别的文本,只要输出格式受FineReader支持. ...

  4. GridView中实现点击某行的任意位置就选中该行

    来源:http://auv2009.blog.163.com/blog/static/68858712200992731010670/ 在 GridView中增加一列:(该列是选择按钮,让其不显示) ...

  5. 【转】ASP.NET Cookies简单应用 记住用户名和密码

    不要试图给Password类型的TextBox赋值! 在asp.net中,不要试图给Password类型的TextBox控件赋值! 无论是在设计或是运行时,都不可以的. 猜测的原因是,password ...

  6. 运行setup.js文件

    C:\Windows\System32>wscript.exe setup.js

  7. Android ListView 第一次设置Adapter时候getView调用多次

    之前遇到这个奇怪现象,记录一下: 使用Listview并设置Adapter时, 会回调多次getView,比如我有4个items,按理说getView应该是调用一次(打出4个log),结果回调有4次( ...

  8. Java OCR 图像智能字符识别技术,可识别中文

    http://www.open-open.com/lib/view/open1363156299203.html

  9. jquery radio的取值 radio的选中 radio的重置

    radio 按钮组, name=”sex”. <input type="radio" name="sex" value="Male"& ...

  10. LintCode "Find Peak Element II"

    Idea is the same: climbing up the hill along one edge (Greedy)! Visualize it in your mind! class Sol ...