浅谈spring ioc的注入方式及注入不同的数据类型

关于Spring-IoC的简单使用参考:

spring ioc的简单实例及bean的作用域属性解析

1、通过set方法注入不同数据类型

测试类代码(set方式注入的属性一定要加set方法)

/**通过set方法注入示例*/
public class IoC_By_Set {
	/**注入Integer类型参数*/
	private Integer id;
	/**注入String类型参数*/
	private String name;
	/**注入实体Bean*/
	private User user;
	/**注入数组*/
	private Object[] array;
	/**注入List集合*/
	private List<Object> list;
	/**注入Set集合*/
	private Set<Object> set;
	/**注入Map键值对*/
	private Map<Object,Object> map;
	/**注入properties类型*/
	private Properties properties;
	/**注入空字符串*/
	private String emptyValue;
	/**注入null值*/
	private String nullValue = "";
	/**检测注入的属性是否全部正确*/
	public Boolean checkAttr() {
		if(id == null) {
			return false;
		} else {
			System.out.println("id:" + id);
		}
		System.out.println("--------------------------");
		if(name == null) {
			return false;
		} else {
			System.out.println("name:" + name);
		}
		System.out.println("--------------------------");
		if(user == null) {
			return false;
		} else {
			System.out.println("Bean:" + user.getId() + "|" + 
			          user.getUserName() + "|" + user.getPassWord());
		}
		System.out.println("--------------------------");
		if(array == null) {
			return false;
		} else {
			System.out.println("array:");
			for (Object object : array) {
				System.out.println(object.toString());
			}
		}
		System.out.println("--------------------------");
		if(list == null) {
			return false;
		} else {
			System.out.println("list:");
			for (Object object : list) {
				System.out.println(object.toString());
			}
		}
		System.out.println("--------------------------");
		if(set == null) {
			return false;
		} else {
			System.out.println("set:");
			for (Object object : set) {
				System.out.println(object.toString());
			}
		}
		System.out.println("--------------------------");
		if(map == null) {
			return false;
		} else {
			Set<Entry<Object,Object>> set = map.entrySet();
			System.out.println("map:");
			for (Entry<Object,Object> entry : set) {
				System.out.println(entry.getKey() + "|" + entry.getValue());
			}
		}
		System.out.println("--------------------------");
		if(properties == null) {
			return false;
		} else {
			Set<Entry<Object,Object>> set = properties.entrySet();
			System.out.println("properties:");
			for (Entry<Object,Object> entry : set) {
				System.out.println(entry.getKey() + "|" + entry.getValue());
			}
		}
		System.out.println("--------------------------");
		if(!"".equals(emptyValue))
		      return false;
		System.out.println("--------------------------");
		if(!(null == nullValue))
		      return false;
		System.out.println("--------------------------");
		System.out.println("全部正确!!!");
		return true;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public void setArray(Object[] array) {
		this.array = array;
	}
	public void setList(List<Object> list) {
		this.list = list;
	}
	public void setSet(Set<Object> set) {
		this.set = set;
	}
	public void setMap(Map<Object,Object> map) {
		this.map = map;
	}
	public void setProperties(Properties properties) {
		this.properties = properties;
	}
	public void setEmptyValue(String emptyValue) {
		this.emptyValue = emptyValue;
	}
	public void setNullValue(String nullValue) {
		this.nullValue = nullValue;
	}
}

applicationContext.xml配置

  <!-- set方式注入 -->
  <bean id="ioC_By_Set" class="com.bc.ioc.demo01.IoC_By_Set">
    <!-- 注入id属性 -->
    <property name="id" value="1"/>
    <!-- 使用<![CDATA[]]>标记处理XML特 殊字符 -->
    <property name="name">
      <!-- 也可以使用P&G -->
      <value><![CDATA[P&G]]></value>
    </property>
    <!-- 定义内部Bean注入 -->
    <property name="user">
      <bean class="com.bc.pojo.User">
        <property name="id" value="1"/>
        <property name="userName" value="内部Bean"/>
        <property name="passWord" value="233"/>
      </bean>
    </property>
    <!-- 注入数组类型 -->
    <property name="array">
      <array>
        <!-- 定义数组元素 -->
        <value>array01</value>
        <value>array02</value>
        <value>array03</value>
      </array>
    </property>
    <!-- 注入List类型 -->
    <property name="list">
      <list>
        <!-- 定义list中元素 -->
        <value>list01</value>
        <value>list02</value>
        <value>list03</value>
      </list>
    </property>
    <!-- 注入Set类型 -->
    <property name="set">
      <set>
        <!-- 定义set中元素 -->
        <value>set01</value>
        <value>set02</value>
        <value>set03</value>
      </set>
    </property>
    <!-- 注入Map类型 -->
    <property name="map">
      <map>
        <!-- 定义map中的键值对 -->
        <entry>
          <key>
            <value>mapKey01</value>
          </key>
          <value>mapValue01</value>
        </entry>
        <entry>
          <key>
            <value>mapKey02</value>
          </key>
          <value>mapValue02</value>
        </entry>
      </map>
    </property>
    <!-- 注入properties类型 -->
    <property name="properties">
      <props>
        <!-- 定义properties中的键值对 -->
        <prop key="propKey1">propValue1</prop>
        <prop key="propKey2">propValue2</prop>
      </props>
    </property>
    <!-- 注入空字符串 -->
    <property name="emptyValue">
      <value></value>
    </property>
    <!-- 注入null值 -->
    <property name="nullValue">
      <null/>
    </property>
  </bean>

测试代码

public class IoC_Test {
	private ApplicationContext ctx;
	@Before
	  public void load() {
		//读取applicationContext.xml配置文件
		ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
	}
	@Test
	  public void SetTest() {
		IoC_By_Set ioc = (IoC_By_Set) ctx.getBean("ioC_By_Set");
		ioc.checkAttr();
	}
}

控制台结果:

id:1
--------------------------
name:P&G
--------------------------
Bean:1|内部Bean|233
--------------------------
array:
array01
array02
array03
--------------------------
list:
list01
list02
list03
--------------------------
set:
set01
set02
set03
--------------------------
map:
mapKey01|mapValue01
mapKey02|mapValue02
--------------------------
properties:
propKey2|propValue2
propKey1|propValue1
--------------------------
--------------------------
--------------------------
全部正确!!!

2、通过构造方法注入各种类型属性

注意:使用JDK1.8版本请将spring相关jar包升级到4.x版本以上,否则不兼容构造方法注入

测试类代码

/** 通过构造方法注入示例 */
public class IoC_By_Constructor {
	private Integer id;
	private String name;
	private User user;
	private List<Object> list;
	public IoC_By_Constructor() {
	}
	public IoC_By_Constructor(Integer id,String name,User user,List<Object> list) {
		this.id = id;
		this.name = name;
		this.user = user;
		this.list = list;
	}
	/**检查是否注入成功*/
	public Boolean checkAttr() {
		if(id == null) {
			return false;
		} else {
			System.out.println("id:" + id);
		}
		System.out.println("----------------------------");
		if(name == null) {
			return false;
		} else {
			System.out.println("name:" + name);
		}
		System.out.println("----------------------------");
		if(user == null) {
			return false;
		} else {
			System.out.println("user:" + user.getId() + "|" + 
			          user.getUserName() + "|" + user.getPassWord());
		}
		System.out.println("----------------------------");
		if(list == null) {
			return false;
		} else {
			System.out.println("list:");
			for (Object object : list) {
				System.out.println(object.toString());
			}
		}
		System.out.println("----------------------------");
		System.out.println("全部正确!!!");
		return true;
	}
}

applicationContext.xml配置

  <!-- 构造方法注入 演示几种类型-->
  <bean id="ioC_By_Constructor" class="com.bc.ioc.demo02.IoC_By_Constructor">
    <!-- 注入Integer属性,可以选择使用index指定参数位置,也可以选择使用type指定参数类型 -->
    <constructor-arg index="0" value="1" type="java.lang.Integer"/>
    <!-- 注入字符串 -->
    <constructor-arg value="P&G"/>
    <!-- 注入对象 -->
    <constructor-arg>
      <!-- 内建对象 -->
      <bean class="com.bc.pojo.User">
        <constructor-arg value="1"/>
        <constructor-arg value="构造内部Bean"/>
        <constructor-arg value="666"/>
      </bean>
    </constructor-arg>
    <!-- 注入集合 -->
    <constructor-arg>
      <list>
        <value>list01</value>
        <value>list02</value>
        <value>list03</value>
      </list>
    </constructor-arg>
  </bean>

测试代码:

public class IoC_Test {
	private ApplicationContext ctx;
	@Before
	  public void load() {
		//读取applicationContext.xml配置文件
		ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
	}
	@Test
	  public void constructorTest() {
		IoC_By_Constructor ioc = (IoC_By_Constructor) ctx.getBean("ioC_By_Constructor");
		ioc.checkAttr();
	}
}

控制台结果:

id:1
----------------------------
name:P&G
----------------------------
user:1|构造内部Bean|666
----------------------------
list:
list01
list02
list03
----------------------------
全部正确!!!

3、自动注入(自动装配)

自动装配虽然能节省一些代码但是不推荐使用

测试类代码:

/**自动装配注入*/
public class IoC_By_Auto {
	private User user;
	/**检查是否注入成功*/
	public Boolean checkAttr() {
		if(user == null) {
			return false;
		} else {
			System.out.println("user:" + user.getId() + "|" + 
			          user.getUserName() + "|" + user.getPassWord());
		}
		System.out.println("正确!!!");
		return true;
	}
	/**自动装配的属性需要设置set方法*/
	public void setUser(User user) {
		this.user = user;
	}
}

applicationContext.xml配置

  <!-- 被自动装配获取的bean -->
  <bean id="user" class="com.bc.pojo.User">
    <property name="id" value="1"/>
    <property name="userName" value="自动装配"/>
    <property name="passWord" value="233"/>
  </bean>
  <!-- 自动装配的bean
     autowire:byName 根据类的属性名查找与之命名相同的id的bean进行装配
         byType 根据类的属性类型查找唯一一个匹配类型的bean,如果有多个bean匹配则抛出异常
         constructor 根据类的构造方法参数类型匹配对应的bean
         no 默认,表示不使用自动装配
         default:由上级标签<beans>的default-autowire属性确定 -->
  <bean id="ioC_By_Auto" class="com.bc.ioc.demo03.IoC_By_Auto" autowire="byName"></bean>

测试代码

public class IoC_Test {
	private ApplicationContext ctx;
	@Before
	  public void load() {
		//读取applicationContext.xml配置文件
		ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
	}
	@Test
	  public void AutoTest() {
		IoC_By_Auto ioc = (IoC_By_Auto) ctx.getBean("ioC_By_Auto");
		ioc.checkAttr();
	}
}

控制台结果

user:1|自动装配|233
正确!!!

以上使用的是byName模式,其他模式配置代码已经注明,不做测试。

4、使用P命名空间注入属性

测试类代码

/**使用P命名空间注入*/
public class IoC_By_P {
	private Integer id;
	private String name;
	private User user;
	/**检查是否注入成功*/
	public Boolean checkAttr() {
		if(id == null) {
			return false;
		} else {
			System.out.println("id:" + id);
		}
		System.out.println("----------------------------");
		if(name == null) {
			return false;
		} else {
			System.out.println("name:" + name);
		}
		System.out.println("----------------------------");
		if(user == null) {
			return false;
		} else {
			System.out.println("user:" + user.getId() + "|" + 
			          user.getUserName() + "|" + user.getPassWord());
		}
		System.out.println("----------------------------");
		System.out.println("全部正确!!!");
		return true;
	}
	//使用P命名空间注入属性需要设置set方法
	public void setId(Integer id) {
		this.id = id;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setUser(User user) {
		this.user = user;
	}
}

applicationContext.xml配置

  <!-- 使用P命名空间注入各种类型属性 -->
  <bean id="user2" class="com.bc.pojo.User">
    <property name="id" value="1"/>
    <property name="userName" value="P"/>
    <property name="passWord" value="233"/>
  </bean>
  <bean id="ioC_By_P" class="com.bc.ioc.demo04.IoC_By_P" p:id="1" 
    p:name="命名空间" p:user-ref="user2"></bean>

测试代码

public class IoC_Test {
	private ApplicationContext ctx;
	@Before
	  public void load() {
		//读取applicationContext.xml配置文件
		ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
	}
	@Test
	  public void PTest() {
		IoC_By_P ioc = (IoC_By_P) ctx.getBean("ioC_By_P");
		ioc.checkAttr();
	}
}

控制台结果

id:1
----------------------------
name:命名空间
----------------------------
user:1|P|233
----------------------------
全部正确!!!

5、使用注解方式注入

Spring在3.0以后,提供了基于Annotation(注解)的注入。

1.@Autowired-对成员变量、方法和构造函数进行标注,来完成自动装配的工作,不推荐使用

2.@Qualifier-配合@Autowired来解决装配多个同类型的bean

3.@Resource-JSR-250标准注解,作用相当于@Autowired,只不过@Autowired按byType自动注入,而@Resource默认按byName自动注入

4.@PostConstruct-在方法上加上注解@PostConstruct,这个方法就会在Bean初始化之后被Spring容器执行

5.@PreDestroy-在方法上加上注解@PreDestroy,这个方法就会在Bean初始化之后被Spring容器执行

6.@Component-只需要在对应的类上加上一个@Component注解,就将该类定义为一个Bean,不推荐使用,推荐使用更加细化的三种:@Repository、@Service、@Controller

@Repository存储层Bean

@Service业务层Bean

@Controller展示层Bean

7.@Scope-定义Bean的作用范围

首先配置applicationContext.xml开启注解

  <!-- 扫描包中注解标注的类 -->
  <context:component-scan base-package="com.bc.ioc.demo05"/>

实体Bean加注解

@Repository
public class User {
	private Integer id = 1;
	private String userName = "注解注入";
	private String passWord = "233";
	public User() {
		super();
	}
	public User(Integer id,String userName,String passWord) {
		super();
		this.id = id;
		this.userName = userName;
		this.passWord = passWord;
	}
	public Integer getId() {
		return id;
	}
	public String getUserName() {
		return userName;
	}
	public String getPassWord() {
		return passWord;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public void setPassWord(String passWord) {
		this.passWord = passWord;
	}
}

测试类代码加注解

/**使用注解注入属性*/
@Service("ioC_By_Annotation")
public class IoC_By_Annotation {
	@Resource
	  private User user;
	public void setUser(User user) {
		this.user = user;
	}
	/**检查是否注入成功*/
	public Boolean checkAttr() {
		if(user == null) {
			return false;
		} else {
			System.out.println("user:" + user.getId() + "|" + 
			          user.getUserName() + "|" + user.getPassWord());
		}
		System.out.println("正确!!!");
		return true;
	}
}

测试代码

public class IoC_Test {
	private ApplicationContext ctx;
	@Before
	  public void load() {
		//读取applicationContext.xml配置文件
		ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
	}
	@Test
	  public void annotationTest() {
		IoC_By_Annotation ioc = (IoC_By_Annotation) ctx.getBean("ioC_By_Annotation");
		ioc.checkAttr();
	}
}

控制台输出

经测试使用注解注入如果applicationContext.xml配置有其他注入方式会报错,也会导致其他注入方式异常。

user:1|注解注入|233
正确!!!

6、通过配置静态工厂方法Bean注入

静态工厂代码

/**静态工厂*/
public class StaticFactory {
	public static Integer getId() {
		return 1;
	}
	public static String getName() {
		return "静态工厂";
	}
	public static User getUser() {
		return new User(1,"工厂User","666");
	}
}

测试类代码

/** 通过静态工厂方式注入 */
public class IoC_By_StaticFactory {
	private Integer id;
	private String name;
	private User user;
	/** 检查是否注入成功 */
	public Boolean checkAttr() {
		if (id == null) {
			return false;
		} else {
			System.out.println("id:" + id);
		}
		System.out.println("----------------------------");
		if (name == null) {
			return false;
		} else {
			System.out.println("name:" + name);
		}
		System.out.println("----------------------------");
		if (user == null) {
			return false;
		} else {
			System.out.println("user:" + user.getId() + "|"
			          + user.getUserName() + "|" + user.getPassWord());
		}
		System.out.println("----------------------------");
		System.out.println("全部正确!!!");
		return true;
	}
	/**需要为需要注入的属性设置set方法*/
	public void setId(Integer id) {
		this.id = id;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setUser(User user) {
		this.user = user;
	}
}

applicationContext.xml配置

  <!-- 配置静态工厂方法Bean 其实就是将工厂方法返回的数值配置成Bean -->
  <bean id="factory_id" class="com.bc.ioc.demo06.StaticFactory" factory-method="getId"/>
  <bean id="factory_name" class="com.bc.ioc.demo06.StaticFactory" factory-method="getName"/>
  <bean id="factory_user" class="com.bc.ioc.demo06.StaticFactory" factory-method="getUser"/>
  <!-- 注入对应的静态工厂方法Bean -->
  <bean id="ioC_By_StaticFactory" class="com.bc.ioc.demo06.IoC_By_StaticFactory">
    <property name="id" ref="factory_id"/>
    <property name="name" ref="factory_name"/>
    <property name="user" ref="factory_user"/>
  </bean>

测试代码

public class IoC_Test {
	private ApplicationContext ctx;
	@Before
	  public void load() {
		//读取applicationContext.xml配置文件
		ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
	}
	@Test
	  public void staticFactoryTest() {
		IoC_By_StaticFactory ioc = (IoC_By_StaticFactory) ctx.getBean("ioC_By_StaticFactory");
		ioc.checkAttr();
	}
}

控制台输出结果

id:1
----------------------------
name:静态工厂
----------------------------
user:1|工厂User|666
----------------------------
全部正确!!!

7、通过实例工厂方法注入

与静态工厂区别在于实例工厂不是静态的,需要先new 一个实例工厂对象,才可以配置其方法,而new 的这个对象也由spring来管理

工厂代码

/**实例工厂*/
public class Factory {
	public Integer getId() {
		return 1;
	}
	public String getName() {
		return "实例工厂";
	}
	public User getUser() {
		return new User(1,"实例工厂User","233");
	}
}

测试类代码

/**实例工厂注入*/
public class IoC_By_Factory {
	private Integer id;
	private String name;
	private User user;
	/** 检查是否注入成功 */
	public Boolean checkAttr() {
		if (id == null) {
			return false;
		} else {
			System.out.println("id:" + id);
		}
		System.out.println("----------------------------");
		if (name == null) {
			return false;
		} else {
			System.out.println("name:" + name);
		}
		System.out.println("----------------------------");
		if (user == null) {
			return false;
		} else {
			System.out.println("user:" + user.getId() + "|"
			          + user.getUserName() + "|" + user.getPassWord());
		}
		System.out.println("----------------------------");
		System.out.println("全部正确!!!");
		return true;
	}
	/**需要为需要注入的属性设置set方法*/
	public void setId(Integer id) {
		this.id = id;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setUser(User user) {
		this.user = user;
	}
}

applicationContext.xml配置

  <!-- 配置实例工厂Bean -->
  <bean id="factory" class="com.bc.ioc.demo07.Factory"/>
  <!-- 配置实例工厂方法Bean -->
  <bean id="f_id" factory-bean="factory" factory-method="getId"/>
  <bean id="f_name" factory-bean="factory" factory-method="getName"/>
  <bean id="f_user" factory-bean="factory" factory-method="getUser"/>
  <!-- 注入对应的实例工厂方法Bean -->
  <bean id="ioC_By_Factory" class="com.bc.ioc.demo07.IoC_By_Factory">
    <property name="id" ref="f_id"/>
    <property name="name" ref="f_name"/>
    <property name="user" ref="f_user"/>
  </bean>

测试类代码

public class IoC_Test {
	private ApplicationContext ctx;
	@Before
	  public void load() {
		//读取applicationContext.xml配置文件
		ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
	}
	@Test
	  public void factoryTest() {
		IoC_By_Factory ioc = (IoC_By_Factory) ctx.getBean("ioC_By_Factory");
		ioc.checkAttr();
	}
}

控制台输出

id:1
----------------------------
name:实例工厂
----------------------------
user:1|实例工厂User|233
----------------------------
全部正确!!!

总结

以上就是本文关于浅谈spring ioc的注入方式及注入不同的数据类型的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢! 目录 连接 连接池产生原因 连接池实现原理 小结 TEMPERANCE:Eat not to dullness;drink not to elevation.节制
摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢! 一个优秀的工程师和一个普通的工程师的区别,不是满天飞的架构图,他的功底体现在所写的每一行代码上。-- 毕玄 1. 命名风格 【书摘】类名用 UpperCamelC
今天犯了个错:“接口变动,伤筋动骨,除非你确定只有你一个人在用”。哪怕只是throw了一个新的Exception。哈哈,这是我犯的错误。一、接口和抽象类类,即一个对象。先抽象类,就是抽象出类的基础部分,即抽象基类(抽象类)。官方定义让人费解,但是记忆方法是也不错的 —包含抽象方法的类叫做抽象类。接口
Writer :BYSocket(泥沙砖瓦浆木匠)微 博:BYSocket豆 瓣:BYSocketFaceBook:BYSocketTwitter :BYSocket一、引子文件,作为常见的数据源。关于操作文件的字节流就是 —FileInputStream&amp;FileOutputStream。
作者:泥沙砖瓦浆木匠网站:http://blog.csdn.net/jeffli1993个人签名:打算起手不凡写出鸿篇巨作的人,往往坚持不了完成第一章节。交流QQ群:【编程之美 365234583】http://qm.qq.com/cgi-bin/qm/qr?k=FhFAoaWwjP29_Aonqz
本文目录 线程与多线程 线程的运行与创建 线程的状态 1 线程与多线程 线程是什么? 线程(Thread)是一个对象(Object)。用来干什么?Java 线程(也称 JVM 线程)是 Java 进程内允许多个同时进行的任务。该进程内并发的任务成为线程(Thread),一个进程里至少一个线程。 Ja
Writer :BYSocket(泥沙砖瓦浆木匠)微 博:BYSocket豆 瓣:BYSocketFaceBook:BYSocketTwitter :BYSocket在面向对象编程中,编程人员应该在意“资源”。比如?1String hello = &quot;hello&quot;; 在代码中,我们
摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢! 这是泥瓦匠的第103篇原创 《程序兵法:Java String 源码的排序算法(一)》 文章工程:* JDK 1.8* 工程名:algorithm-core-le
摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢! 目录 一、父子类变量名相同会咋样? 有个小故事,今天群里面有个人问下面如图输出什么? 我回答:60。但这是错的,答案结果是 40 。我知错能改,然后说了下父子类变
作者:泥瓦匠 出处:https://www.bysocket.com/2021-10-26/mac-create-files-from-the-root-directory.html Mac 操作系统挺适合开发者进行写代码,最近碰到了一个问题,问题是如何在 macOS 根目录创建文件夹。不同的 ma
作者:李强强上一篇,泥瓦匠基础地讲了下Java I/O : Bit Operation 位运算。这一讲,泥瓦匠带你走进Java中的进制详解。一、引子在Java世界里,99%的工作都是处理这高层。那么二进制,字节码这些会在哪里用到呢?自问自答:在跨平台的时候,就凸显神功了。比如说文件读写,数据通信,还
1 线程中断 1.1 什么是线程中断? 线程中断是线程的标志位属性。而不是真正终止线程,和线程的状态无关。线程中断过程表示一个运行中的线程,通过其他线程调用了该线程的 方法,使得该线程中断标志位属性改变。 深入思考下,线程中断不是去中断了线程,恰恰是用来通知该线程应该被中断了。具体是一个标志位属性,
Writer:BYSocket(泥沙砖瓦浆木匠)微博:BYSocket豆瓣:BYSocketReprint it anywhere u want需求 项目在设计表的时候,要处理并发多的一些数据,类似订单号不能重复,要保持唯一。原本以为来个时间戳,精确到毫秒应该不错了。后来觉得是错了,测试环境下很多一
纯技术交流群 每日推荐 - 技术干货推送 跟着泥瓦匠,一起问答交流 扫一扫,我邀请你入群 纯技术交流群 每日推荐 - 技术干货推送 跟着泥瓦匠,一起问答交流 扫一扫,我邀请你入群 加微信:bysocket01
Writer:BYSocket(泥沙砖瓦浆木匠)微博:BYSocket豆瓣:BYSocketReprint it anywhere u want.文章Points:1、介绍RESTful架构风格2、Spring配置CXF3、三层初设计,实现WebService接口层4、撰写HTTPClient 客户
Writer :BYSocket(泥沙砖瓦浆木匠)什么是回调?今天傻傻地截了张图问了下,然后被陈大牛回答道“就一个回调…”。此时千万个草泥马飞奔而过(逃哈哈,看着源码,享受着这种回调在代码上的作用,真是美哉。不妨总结总结。一、什么是回调回调,回调。要先有调用,才有调用者和被调用者之间的回调。所以在百
Writer :BYSocket(泥沙砖瓦浆木匠)一、什么大小端?大小端在计算机业界,Endian表示数据在存储器中的存放顺序。百度百科如下叙述之:大端模式,是指数据的高字节保存在内存的低地址中,而数据的低字节保存在内存的高地址中,这样的存储模式有点儿类似于把数据当作字符串顺序处理:地址由小向大增加
What is a programming language? Before introducing compilation and decompilation, let&#39;s briefly introduce the Programming Language. Programming la
Writer :BYSocket(泥沙砖瓦浆木匠)微 博:BYSocket豆 瓣:BYSocketFaceBook:BYSocketTwitter :BYSocket泥瓦匠喜欢Java,文章总是扯扯Java。 I/O 基础,就是二进制,也就是Bit。一、Bit与二进制什么是Bit(位)呢?位是CPU
Writer:BYSocket(泥沙砖瓦浆木匠)微博:BYSocket豆瓣:BYSocket一、前言 泥瓦匠最近被项目搞的天昏地暗。发现有些要给自己一些目标,关于技术的目标:专注很重要。专注Java 基础 + H5(学习) 其他操作系统,算法,数据结构当成课外书博览。有时候,就是那样你越是专注方面越